r - How do i refer to rows/ columns in data frames that are within a list? -
i'm looking work data frames have within list. i'm getting 'incorrect number of subscripts' , similar errors, despite current best efforts. here's code:
folder = 'c:/path csv files-071813/' symbs = c('spx', 'xlf', 'xly', 'xlv', 'xli', 'iyz', 'xlp', 'xle', 'xlk', 'xlb', 'xlu', 'shv') importdata = vector('list', length(symbs)) names(importdata) = symbs (sidx in 1:length(symbs)){ #import data each symbol list. importdata[sidx] = read.csv(paste(folder, symbs[sidx], '.csv', sep = ''), header = true) } each csv file thousands of rows, , 7 columns. i'm assuming have above returning data frame each csv file, list. i'd enter:
importdata[[1]][, 1]
to work first column of first data frame in list. close? can't find resolution despite searching. many in advance...
the apply family of functions going friend here, lapply, function which, given list , function, applies function every element of list , returns results elements of new list.
folder = 'c:/path csv files-071813/' symbs = c('spx', 'xlf', 'xly', 'xlv', 'xli', 'iyz', 'xlp', 'xle', 'xlk', 'xlb', 'xlu', 'shv') filenames = paste0(folder,symbs,'.csv') listofdataframes=lapply(filenames,read.table,header=t) now if want second column dataframes
listoffirstcols=lapply(listofdataframes,"[",,2) or more explicitly
listoffirstcols=lapply(listofdataframes,function(x)x[,1])
Comments
Post a Comment