r - open many file in one time and each store as one dataframe -
i have many data in 1 folder
data=list.files(path="d:/result")
data
> > [1] "cnvworkshop (1).txt" "e17 result.txt" [3] "e17new result.txt" "e18 new result.txt" > > [5] "e18result.txt" "l15 new result.txt" [7] "l15 > result.txt" "l22new result.txt" [9] "l51 new result.txt" > "l51result.txt" [11] "l54 new result.txt" "l54result.txt" > [13] "penncnv.txt" "s40 new result.txt"
i want read many file in 1 time , store each file each dataframe
a=paste("watto",1:14,sep="")
this things below it's not work
for(i in 1 :length(data)){ a[i]=read.table(file=paste("d:/result/",data[i],sep=""),header=t,sep="\t") }
and it's show warning message , not give me result
> warning message: in a[1] = read.table(file = paste("d:/result/", data[1], sep = ""), : number of items replace not multiple of replacement length
the result want is
> data watto1 come cnvworkshop (1).txt > data watto2 come e17 result.txt
how should do?
try:
<- lapply(data, function(x) read.table(file=paste0("d:/result/",x), header=true, sep="\t") )
you're getting error message because of r's subsetting rules. when assign single brackets a[1] <-
, r expects replacement of same length 1. a[c(1,2)] <-
r accepts replacement values of length 2, , on. data frame has length (being number of columns) greater 1 , being rejected.
you instead specify replacing list element (note double-brackets:
for(i in 1 :length(data)){ a[[i]]=read.table(file=paste("d:/result/",data[i],sep=""), header=true,sep="\t") }
however, not r-like syntax. iterative tasks not rely on recursion can performed via lapply
family of functions.
Comments
Post a Comment