12.07.2015 Views

R dummies

R dummies

R dummies

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

with(mtcars, length(mpg[hp > 150]))[1] 13Transposing Columns or RowsSometimes you need to transpose your data from rows to columns or viceversa. In R, the function to transpose a matrix is t():> x x[,1] [,2] [,3][1,] 1 5 9[2,] 2 6 10[3,] 3 7 11[4,] 4 8 12To get the transpose of a matrix, use t():> t(x)[,1] [,2] [,3] [,4][1,] 1 2 3 4[2,] 5 6 7 8[3,] 9 10 11 12You also can use t() to transpose data frames, but be careful when you dothis. The result of a transposition is always a matrix (or array). Because arraysalways have only one type of variable, such as numeric or character, thevariable types of your results may not be what you expect.See how the transposition of cars is a character array:> t(mtcars[1:4, ])Mazda RX4 Mazda RX4 Wag Datsun 710 Hornet 4 Drivempg “21.0” “21.0” “22.8” “21.4”cyl “6” “6” “4” “6”disp “160” “160” “108” “258”hp “110” “110” “ 93” “110”drat “3.90” “3.90” “3.85” “3.08”wt “2.620” “2.875” “2.320” “3.215”qsec “16.46” “17.02” “18.61” “19.44”vs “0” “0” “1” “1”am “1” “1” “1” “0”gear “4” “4” “4” “3”carb “4” “4” “1” “1”

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!