问题
I have a txt file with the following structure:
NAME DATA1 DATA2
a 10 1,2,3
b 6 8,9
c 20 5,6,7 ,8
The first line represent the header and the data is separated by tabs. I need to put the elements of DATA1
in a list or vector in a way that I can traverse the elements one by one.
Also I need to extract the elements of DATA2
for each NAME
and to put them in a list so I can traverse then individually, e.g. get the elements 8,9 for NAME
b and put it into a list. (Note that the third record has a space in the list in DATA2
between the 7
and the comma).
How I can do that both operations? I know that I can use read.table
and $
for accessing individual elements, but I am stuck.
info<-read.table("table1", header=FALSE,sep="\t")
namelist<-list(info$NAME)
回答1:
Run this demo and look at the structure of n
, d1
, and d2
-- that should help you get going:
df = read.table(text="NAME\tDATA1\tDATA2
a\t10\t1,2,3
b\t6\t8,9
c\t20\t5,6,7 ,8",
header= TRUE,
stringsAsFactors=FALSE,
sep='\t')
n = df$NAME
d1 = df$DATA1
d2 = lapply(strsplit(df$DATA2, ","),
as.numeric)
names(d2) = n
d2['b'][1] # access first element in list named 'b'
lapply(d2, FUN=mean) # mean of all rows in d2
来源:https://stackoverflow.com/questions/11470734/reading-a-table-in-r