[R] lapply getting names of the list
dwinsemius at comcast.net
Thu Dec 9 20:44:32 CET 2010
On Dec 9, 2010, at 2:21 PM, David Winsemius wrote:
> On Dec 9, 2010, at 12:44 PM, Sashi Challa wrote:
>> Hello All,
>> I have a toy dataframe like this. It has 8 columns separated by tab.
>> Name SampleID Al1 Al2 X Y R Th
>> rs191191 A1 A B 0.999 0.09 0.78 0.090
>> abc928291 A1 B J 0.3838 0.3839 0.028 0.888
>> abcnab A1 H K 0.3939 0.939 0.3939 0.77
>> rx82922 B1 J K 0.3838 0.393 0.393 0.00
>> rcn3939 B1 M O 0.000 0.000 0.000 0.77
>> tcn39399 B1 P I 0.393 0.393 0.393 0.56
Those were not tabs after being processed by various portions of the
various mail systems.
>> Note that the SampleID is repeating. So I want to be able to split
>> the dataset based on the SampleID and write the splitted dataset of
>> every SampleID into a new file.
>> I tried split followed by lapply to do this.
>> infile <- read.csv("test.txt", sep="\t", as.is = TRUE, header = TRUE)
>> infile.split <- split(infile, infile$SampleID)
>> names(infile.split) ## outputs “A1”
>> ## now A1, B1 are two lists in infile.split as I understand it.
>> Correct me if I am wrong.
See if this works any better:
filename <- deparse(substitute(x)) # this is the way to
recover the "names" of arguments
final_filename <- paste(filename,"toy_set.txt", sep="_")
write.table(x, file = paste("", final_filename,sep="/"),
I substituted "" for that path variable that you didn't provide, put
in a missing ")" in the write.table file=paste() that wasmissing, and
I substituted regular double quotes for those damnable smart-quotes
that _your_ mailer inserted.
>> In lapply I wanted to give a unique filename to all the split
>> Sample Ids, i.e. name them here as <dragged to the c() construct>.
>> How do I get those names, i.e. A1, B1 to a create a filename like
> names(file.split) <- c("A1_toy_set.txt", "B1_toy_set_txt")
>> When I write each of the element in the list obtained after split
>> into a file,
> How are you proposing do do this "writing"?
>> the column names would have names like A1.Name, A1.SampleID,
>> A1.Al1, …..
> Are you sure? Why would you think that?
>> Can I get rid of “A1” in the column names within the lapply (other
>> than reading in the file again and changing the names) ?
>> Thanks for your time,
>> [[alternative HTML version deleted]]
>> R-help at r-project.org mailing list
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> David Winsemius, MD
> West Hartford, CT
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
David Winsemius, MD
West Hartford, CT
More information about the R-help