Please help with R-studio, what codes would I use? Please type in ready to put i
ID: 3351641 • Letter: P
Question
Please help with R-studio, what codes would I use? Please type in ready to put into R studio.
Find a data set called “matrix”.
(a) Use the function read.table to import the matrix stored in “matrix” into R. Make
sure you assign it to some variable for further usage. It will take a couple of seconds
for R to read it.
(b) Use the command class() and report the type of the imported dataset.
(c) Run the command ?as.matrix in R and learn the usage of this function. Then use the function on your imported dataset. Again use the function class() and report the type.
(d) Use the function dim to report the number of rows and the number of columns of the matrix.
(e) Report the means of the first 5 columns (so we have 5 means). (Hint: Indexing the submatrix first then use function colMeans or apply )
(f) Report the maximum of first 5 rows (so we have 5 maximums). (Hint: Indexing the submatrix first then use function apply )
(18 points)
**DATA SET IS BELOW***
"V1" "V2" "V3" "V4" "V5" "V6" "V7" "V8" "V9" "V10"
"1" 7.6341871120967 3.06787910405546 7.90843046410009 2.01896583288908 2.0677924444899 4.89914236404002 7.1148896929808 7.49517368525267 6.21505601424724 2.87918072938919
"2" 6.03544045705348 2.25033367145807 4.37373606860638 6.55575524643064 5.43112526554614 3.85796774504706 2.85589408688247 2.06616840744391 5.00040261633694 6.10108891688287
"3" 5.85587366577238 3.05149663519114 6.59532016376033 4.12190552754328 6.51449149893597 4.78281086310744 6.10803203983232 7.06587094860151 6.25121828261763 5.99176421901211
"4" 2.87264863960445 2.71294216159731 2.04076345125213 5.70661403285339 3.99310070555657 3.24589099921286 5.1376590244472 5.0494546466507 3.74124456755817 5.67529067769647
"5" 7.19995263544843 6.03290755068883 7.13962257280946 4.27657629363239 3.64152398006991 3.77363716717809 2.0275420579128 2.73017898388207 7.21865665400401 2.67094426974654
"6" 6.50705113913864 4.96567738614976 5.03134351642802 5.12042215047404 2.24256303953007 5.34875761624426 2.83196238568053 6.51267285784706 4.35047051636502 4.21736092492938
"7" 4.50327779538929 5.2951627173461 5.81610233290121 6.87042102357373 6.76283550076187 2.9725723862648 4.07296954188496 4.21178946271539 2.55299962125719 5.55501863034442
"8" 3.97334129363298 3.44581202277914 2.64254210144281 4.18836274184287 6.55116121610627 6.86632635211572 4.33525182446465 6.08209082484245 4.17881360277534 7.78995364997536
"9" 4.3117951201275 3.15255359839648 7.24926634179428 7.88473897287622 5.43226483138278 7.58694769069552 3.35334944818169 3.90150361554697 4.41709932778031 3.75161611381918
"10" 4.73159887082875 4.43860953906551 5.82591924164444 5.42972571682185 7.45301394583657 7.96287086233497 3.73042398877442 2.18637460656464 7.42006577691063 4.98706747638062
"11" 7.42779712565243 4.05783609533682 6.36103554163128 4.4500911203213 2.08544004708529 2.30727399466559 3.60968993371353 7.0277478499338 6.7283415608108 7.41977127594873
"12" 3.91822453029454 7.33986411616206 5.00766927236691 2.95973252039403 7.40176892466843 7.73012174479663 2.55007120780647 7.97743326518685 7.22630992950872 7.54229682171717
"13" 2.20744960429147 5.02137547079474 7.67514779791236 7.25010680500418 5.41333905281499 6.53765423875302 6.04954387806356 4.58857842767611 3.8379403357394 6.08507523220032
"14" 6.56570841278881 5.50958879478276 3.49368588859215 3.02068459009752 3.3330756961368 5.73612142074853 4.4552780627273 4.46462571201846 4.43119475804269 7.0007112766616
"15" 2.6801890200004 2.62162312166765 4.74216886004433 5.55377473495901 5.15887248702347 5.16444447590038 7.33126065135002 7.48587313387543 4.70917429029942 3.05389007506892
"16" 4.25802314933389 3.23492376925424 6.79214454768226 3.11491477116942 3.22129477513954 4.69100463064387 4.05529619148001 5.98410131130368 5.53436738951132 4.22503892146051
"17" 2.73996732011437 3.44099947204813 4.04169837385416 5.48874572012573 7.45422206167132 3.26433765841648 5.09069859888405 3.18795887753367 5.43177974689752 3.11250361753628
"18" 5.59304100228474 3.09108136314899 6.95592224039137 2.54111946653575 2.21604388114065 3.66451922571287 5.95384064456448 2.84642726369202 4.91546678822488 6.68635992147028
"19" 3.15547606069595 6.96394738322124 2.69489595480263 2.57419772818685 2.08488816022873 5.34164644312114 5.86284228507429 7.93372093187645 5.34146670717746 7.30176875879988
"20" 5.15329764410853 5.70845434395596 4.23548742337152 7.67906089639291 3.06018379330635 6.10598225798458 3.03442095639184 4.44640256743878 5.24235906591639 4.61174802808091
"21" 7.07336762081832 3.82137258490548 7.50551739800721 3.42976852646098 3.29865503776819 3.32779399072751 3.50003307359293 4.28855691384524 2.52753399824724 7.4231801177375
"22" 7.66924131242558 2.85213830973953 2.88397772889584 7.43684834055603 4.87450262811035 6.71667457744479 7.67358215898275 4.40665280539542 2.05398029461503 7.01102764904499
"23" 6.07656705006957 2.15657481597736 4.4445057134144 7.56967165647075 6.58037127694115 6.19218277698383 5.5376414321363 7.74608685774729 6.46893179696053 4.85413327626884
"24" 6.70790068199858 5.3599154939875 4.58547488832846 5.94931208528578 6.37212294852361 2.51075429841876 6.70860171783715 6.80677125789225 2.14814897766337 2.45781176863238
"25" 5.15741997025907 2.19863598560914 7.50920908153057 3.89978704228997 3.39634092245251 3.55017964495346 5.41477263625711 5.95083614857867 2.70444820774719 2.3563485709019
"26" 6.53288760548458 6.33674239879474 6.93357034912333 2.00402366556227 3.83411777857691 4.71341485297307 7.14179279701784 2.86436864780262 7.2312652352266 5.26445344975218
"27" 6.86037800461054 3.08843477442861 6.48978878837079 4.80205451976508 4.37193629937246 2.74271604791284 2.63977057626471 4.69677415862679 6.48645102791488 7.57461868785322
"28" 4.78613051166758 7.91748801991343 7.91386718582362 5.94306330662221 5.59079925948754 7.60887814825401 5.3616905072704 5.85898503800854 5.53144568577409 4.88984696613625
"29" 7.88225564407185 2.25047553237528 3.96888283640146 6.84482039883733 6.32769183488563 7.2425066945143 4.30683041829616 5.67375850630924 2.44530711742118 3.6671392256394
"30" 3.07571082329378 3.25043501053005 2.30759339546785 5.90462794154882 5.00179955549538 2.0284323473461 7.0658150035888 3.3291805125773 2.22765388572589 2.90569300344214
"31" 2.68430869746953 5.78817629022524 6.18687190674245 4.06067881081253 3.32747208932415 6.96471171360463 3.53567103855312 3.94126976840198 3.22125583700836 6.11532957479358
"32" 3.22837233683094 5.91912458976731 5.71063755871728 4.38642725488171 6.00692594330758 7.30248616589233 5.87614054884762 4.57257855636999 5.53444737242535 4.97250350192189
"33" 5.54717309074476 5.61503247870132 6.03778609912843 6.2543640807271 4.29926793137565 5.09462657524273 4.85209010215476 6.50803693197668 2.53793320478871 2.33512812061235
"34" 6.21614190656692 7.51016225898638 4.22850363561884 5.38416535127908 5.63457141583785 2.73472455702722 6.44890277693048 6.17168161692098 2.89696858497337 2.47416391735896
"35" 2.37080395501107 2.08364006690681 2.83846207382157 3.47308127069846 3.4605526891537 7.71277405926958 3.86825869232416 7.20855350466445 2.50796773470938 4.67231933167204
"36" 3.11455022962764 4.80520890653133 6.91959314560518 6.05339979520068 2.18722799327224 5.72195898834616 2.23468876257539 7.34877503896132 5.98989601433277 2.88740021921694
"37" 3.21732673607767 5.50324777606875 5.32167932204902 2.36557717202231 4.40312059037387 5.97108771372586 4.65431439317763 4.58555702958256 4.4853288368322 2.26063341833651
"38" 6.57875387137756 5.82048869878054 4.51467729127035 7.11295751063153 5.33011199021712 6.48168467124924 7.63687980407849 7.27147814072669 5.6594707001932 2.7134937858209
"39" 3.09349465556443 2.54642969975248 6.46420467738062 5.6960888588801 3.7293926179409 5.7819012189284 7.03576550353318 7.46826867712662 2.1835972154513 7.04263421054929
"40" 7.07482346799225 7.07637241575867 4.31785764591768 7.00688805896789 3.84371798438951 5.54053351981565 3.4720463543199 3.48730204254389 2.75878441333771 4.23314484348521
"41" 6.11087824776769 6.56810374138877 5.49981698347256 5.30349253257737 4.17773932497948 5.67858256958425 6.83550531323999 5.71726336237043 7.77296551736072 3.60043682251126
"42" 6.76919316593558 2.88095042156056 2.89390574395657 3.40460925363004 4.81180183356628 5.6199111752212 6.16540845669806 2.87053825519979 6.65259710839018 7.34175259247422
"43" 4.69855239614844 5.78083008434623 2.35428868047893 4.98713246826082 4.26405384764075 5.54012307478115 2.46451536612585 4.65529715036973 3.52257641311735 2.37969007575884
"44" 4.54953172150999 2.50728659471497 3.40449667908251 5.14370286138728 5.70069385971874 6.26878577377647 5.50532376253977 7.883814397268 6.15481736045331 3.50632973806933
"45" 6.25296546518803 2.74507326958701 4.55355794075876 6.11553116654977 3.45368398353457 4.90347848739475 5.74830913264304 5.56927362829447 7.88481818186119 3.7628369089216
"46" 3.7651258376427 2.24134210916236 4.89618131984025 5.97923197597265 4.00650326814502 4.00034217536449 7.42358097527176 4.19389845244586 4.88521472690627 5.6571042612195
"47" 5.37582539673895 5.5299481106922 7.48941954644397 6.42913472885266 5.28041143668815 5.34063838096336 2.03653784748167 3.16418304061517 5.85258686030284 3.95369841530919
"48" 7.96233552834019 6.95075853122398 2.78921075398102 4.39881174312904 2.46773560810834 6.39241101685911 6.86252677394077 3.39271779730916 5.56888756202534 3.28119809413329
"49" 6.28854564810172 5.43288156623021 6.06624554377049 6.33933347137645 3.46966440463439 5.29759526019916 6.37403081916273 2.48383584991097 5.49475212069228 6.06304740346968
"50" 5.44888239866123 2.08364526368678 2.86088523222134 7.38231497723609 7.17087265057489 4.55758511694148 2.61232036724687 7.26118596829474 2.94795254478231 7.4438252216205
Explanation / Answer
Here go the R codes (bold and italicized) for the above questions on dataset "matrix". The codes can be tried out in R studio.
Firstlt, we import the matrix data (txt file) using the read.table function. The file.choose function is used to obtain the path of the file.
file.choose ( )
m <- read.table ( "C:\Users\elcot\Documents\chegg\matrix.txt" , header = T )
m
The class() function tells us that the dataset is a data.frame file.
class ( m )
?as.matrix gives us the help for the function with suitable illustrations. By applying that function, the data frame is converted to a matrix. Let it be m1.
?as.matrix
m1 = as.matrix ( m )
m1
The class() function tells us that now its a matrix.
class ( m1 )
dim() function gives the dimension of the matrix as consisting of 10 columns (V1 : V10) in 50 rows.
dim ( m1 )
By using the colMeans() function on the first 5 columns of the matrix, we get the respective means.
colMeans ( m1 [ ,1:5 ] )
The apply function applies the max function on all the rows (1) of the matrix. Since we require only first 5 rows, it is obtained by using the subsetting operator [ ].
maxi = apply ( m1 , 1 , max )
maxi [ 1:5 ]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.