[R] reading data
arun
smartpink111 at yahoo.com
Fri Feb 15 18:18:11 CET 2013
Hi,
#working directory data1 #changed name data to data1. Added some files in each of sub directories a1, a2, etc.
indx1<- indx[indx!=""]
lapply(indx1,function(x) list.files(x))
#[[1]]
#[1] "a1.txt" "mmmmm11kk.txt"
#[[2]]
#[1] "a2.txt" "mmmmm11kk.txt"
#[[3]]
#[1] "a3.txt" "mmmmm11kk.txt"
#[[4]]
#[1] "b1.txt" "mmmmm11kk.txt"
#[[5]]
#[1] "b2.txt" "b3.txt" "mmmmm11kk.txt"
[[6]]
[1] "c1.txt" "c2.txt" "c3.txt" "c4.txt"
[5] "mmmmm11kk.txt"
res<-do.call(c,lapply(list.files(recursive=T)[grep("mmmmm11kk",list.files(recursive=T))],function(x) {names(x)<-gsub("^(.*)\\/.*","\\1",x); lapply(x,function(y) read.table(y,header=TRUE,stringsAsFactors=FALSE,fill=TRUE))})) #it seems like one of the rows of your file doesn't have 6 elements, so added fill=TRUE
head(res,2)
#$a1
# Id M mm x b u k j y p v
#1 aAA 1 2 739 0.1257000 2 2 AA 2 8867 8926
#2 aAAAA 1 2 2263 0.0004000 2 2 AR 4 7640 8926
#3 aA 2 1 1 0.0845435 2 AA 2 6790 734,1092 NA
#4 aAA 1 2 1965 0.0007000 4 3 AR 2 11616 8926
#5 aAAA 1 3 3660 0.0008600 18 3 AA 2 20392 496
#6 AA na 2 1972 0.0007000 11 3 AR 25 509 734
#$a2
# Id M mm x b u k j y p v
#1 aAA 1 2 739 0.1257000 2 2 AA 2 8867 8926
#2 aAAAA 1 2 2263 0.0004000 2 2 AR 4 7640 8926
#3 aA 2 1 1 0.0845435 2 AA 2 6790 734,1092 NA
#4 aAA 1 2 1965 0.0007000 4 3 AR 2 11616 8926
#5 aAAA 1 3 3660 0.0008600 18 3 AA 2 20392 496
#6 AA na 2 1972 0.0007000 11 3 AR 25 509 734
If you want the names to be group_a, group_b etc.
names(res)<-paste("group_",gsub("\\d+","",names(res)),sep="")
res[grep("group_b",names(res))]
$group_b
# Id M mm x b u k j y p v
#1 aAA 1 2 739 0.1257000 2 2 AA 2 8867 8926
#2 aAAAA 1 2 2263 0.0004000 2 2 AR 4 7640 8926
#3 aA 2 1 1 0.0845435 2 AA 2 6790 734,1092 NA
#4 aAA 1 2 1965 0.0007000 4 3 AR 2 11616 8926
#5 aAAA 1 3 3660 0.0008600 18 3 AA 2 20392 496
#6 AA na 2 1972 0.0007000 11 3 AR 25 509 734
#$group_b
# Id M mm x b u k j y p v
#1 aAA 1 2 739 0.1257000 2 2 AA 2 8867 8926
#2 aAAAA 1 2 2263 0.0004000 2 2 AR 4 7640 8926
#3 aA 2 1 1 0.0845435 2 AA 2 6790 734,1092 NA
#4 aAA 1 2 1965 0.0007000 4 3 AR 2 11616 8926
#5 aAAA 1 3 3660 0.0008600 18 3 AA 2 20392 496
#6 AA na 2 1972 0.0007000 11 3 AR 25 509 734
A.K.
----- Original Message -----
From: "veracosta.rt at gmail.com" <veracosta.rt at gmail.com>
To: smartpink111 at yahoo.com
Cc:
Sent: Friday, February 15, 2013 9:15 AM
Subject: reading data
Hi,
I post yesterday and you helped me. I have little problem.
At first, I never worked with regular expressions...
The code that you gave me it's ok, but my files are inside the folders a1,a2,a3. I try to explain better.
I have one folder named "data". Inside this folder I have some other folders named "a1","a2","b1",b2",...and inside of each one of that I have some files. I want only the file "mmmmmm.txt" (in all folders I have One file with this name).
The name of the folder give me the name of the group,but I need to read the file inside. And after, have "group_a", group_"b"...because I need to work with this data grouped (and know the name of the group).
Thank you.
More information about the R-help
mailing list