I am trying to read many files into R to evaluate the data in column 14 of each into its own histogram. It seems to work up until I try to access the actual data (aside from the column names.)
The code seems to work to an extent:
test<-sapply(1:Num.Files,function(x){readLines(File.names[x])})
head(test)
data<-read.table(header=TRUE,text=test[1])
head(data)
names(data)[14]<-'column14'
names(data)
data$column14
but here is the result:
test<-sapply(1:Num.Files,function(x){readLines(File.names[x])}) #Enters the data
head(test) #Can see here that the information is all there
[,1]
[1,] “Targ cov av_cov 87A_cvg 87Ag 87Agr 87Agr 87A_gra 87A%_1 87A%_3 87A%_5 87A%_10 87A%_20 87A%_30 87A%_40\t87A%_50\t87A%_75\t87A%_100”
[2,] “1:028 400\t0.42\t400\t0.42\t1\t1\t2\t41.8\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0”
[3,] “1:296 400\t0.42\t400\t0.42\t1\t1\t2\t41.8\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0”
[4,] “1:453 1646\t8.11\t1646\t8.11\t7\t8\t13\t100.0\t100.0\t87.2\t32.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0”
[5,] “1:427 1646\t8.11\t1646\t8.11\t7\t8\t13\t100.0\t100.0\t87.2\t32.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0”
[6,] “1:736 5105\t29.68\t5105\t29.68\t14\t29\t48\t100.0\t100.0\t100.0\t86.0\t65.7\t49.4\t35.5\t16.9\t0.0\t0.0”
data<-read.table(header=TRUE,text=test[1])
head(data) #all the correct column names
[1] Targ cov av_cov X87A_cvg X87Ag X87Agr X87Agr.1
[8] X87A_gra X87A._1 X87A._3 X87A._5 X87A._10 X87A._20 X87A._30
[15] X87A._40 X87A._50 X87A._75 X87A._100
<0 rows> (or 0-length row.names)
names(data)[14]<-‘column14’
names(data) #column 14 is changed as hoped
[1] “Targ” “cov” “av_cov” “X87A_cvg” “X87Ag” “X87Agr”
[7] “X87Agr.1” “X87A_gra” “X87A._1” “X87A._3” “X87A._5” “X87A._10”
[13] “X87A._20” “column14” “X87A._40” “X87A._50” “X87A._75” “X87A._100”
data$column14 #but there is nothing in the column
logical(0)
Am I using read.table incorrectly? Does anyone have a suggestion?
Yes, you are using read.table incorrectly, you’re passing it only the header line (
text[1]).I’d call
read.tabledirectly on the file names. If you insist on passing the result ofreadLines, you probably want to re-join everything likepaste(test, collapse="\n").