dataframe - how to view head of as.data.frame in R? -
i have huge data set 20 columns , 20,000 rows, according manual of program use, have put data data frame, though i'm not understand does.. , can't seem view head data frame created.
i wrote in bold part don't understand, i'm new r, can kind mind explain me how following works?
first read csv file
vdata = read.csv("my_matrix.csv"); 1) here create data frame per manual, what -c(1:8) exactly??
dataexpr0 = as.data.frame(t(vdata[, -c(1:8)])) 2) here, understand above part does, tried view header of data frame, following line, display first 2 columns 20,000 rows of data. is there way view first 2 rows?
head(dataexpr0, n = 2)
let's disect call doing, inside out.
basic indexing
when indexing data.frame or matrix (assuming 2 dimensions), access single element of square bracket notation, you're seeing. instance, see value in fourth row, fifth column, you'd use vdata[4,5]. can work ranges of rows and/or columns well, such vdata[1:4,5] returning first 4 rows , 5th column vector.
note: range 1:4 can arbitrary vector of numbers, such vdata[c(1,2,5),c(4,8)] returns 3 2 matrix.
btw: default, when resulting slice/submatrix has 1 of dimensions reduced 1 (as in latter example), r drop lower structure (e.g., matrix -> vector -> scalar). in case, drop vdata[1:4,5] vector. can prevent happening adding appears third dimension square brackets: vdata[1:4,5,drop=false], meaning "do not drop simplified dimension". now, should matrix of 4 rows , 1 column in return.
you can read more thorough explanation of how subset data.frames reading (for example) of "hadleyverse". if that, highly encourage make interactive session: play in r read, cement methods.
negative indexing
negative indices mean "everything except listed". in example, subsetting data extract except columns 1:8. vdata[,-c(1:8)] returning rows , columns 9 through 20, 20k 12 matrix. not small.
transposition
you know t() does: transpose matrix 12 20k.
a word of warning: if of data.frame columns of same class (e.g., 'character', 'logical'), fine. however, fact data.frames allow disparate types of data in different columns not feature shared matrices. if 1 data.frame column different others, converted highest common format, e.g., logical < integer < numeric < character.
back data.frame
after transpose (which converts matrix), convert data.frame, may or may not necessary depending on how intend deal data later. instance, if row names not meaningful, may not useful convert data.frame. that's relatively immaterial, i'm fan of not over-converting things. i'm fan of using simpler data structure, , matrices typically faster data.frames.
head
... merely gives top n rows of data.frame or matrix. in case, since transposed it, 20k columns wide, may bit unwieldy on command line.
alternatives
based on provided earlier, perhaps want @ top few rows , first few columns? dataexpr0[1:5,1:5] work, (identically) head(dataexpr0[,1:5], n=5).
more questions?
i encourage read more of hadleyverse , become little more familiar subsetting , basic data management. fundamental using r, , stackoverflow not patient enough answer baseline questions this. forum best suited have done research, read documentation , pages, , tried code, , after cannot figure out why not working. provided basic code good, not ideally suited teach how start r.
Comments
Post a Comment