Skip to content

lapply getting names of the list

5 messages · Sashi Challa, Joshua Wiley, David Winsemius +1 more

#
Hi Sashi,
On Thu, Dec 9, 2010 at 9:44 AM, Sashi Challa <challa at ohsu.edu> wrote:
correct, names() returns the top level names of infile.split (i.e.,
the two data frame names)
It is a single, named list containing two data frames (A1 and B1)
(though data frames are built from lists, I think so I suppose in a
way it contains two lists, but that is not really the point).
by using lapply() on the actual object, your function is getting each
element of the list.  That is:

infile.split[[1]]
infile.split[[2]]

trying names() on those:

names(infile.split[[1]])

should show what you are getting
FYI I think you are missing a parenthesis in there somewhere
Try this:

## read your data from the clipboard (obviously you do not need to)
infile <- read.table("clipboard", header = TRUE)
split.infile <- split(dat, dat$SampleID) #split data
path <- "~" # generic path

## rather than applying to the data itself, instead apply to the names
lapply(names(split.infile), function(x) {
  write.table(x = split.infile[[x]],
    file = paste(path, paste(x, "toy_set.txt", sep = "_"), sep = "/"),
    row.names = FALSE, quote = FALSE, sep = "\t")
  cat("wrote ", x, fill = TRUE)
})

it will return two NULL lists, but that is fine because it should have
written the files.
Can you report the results of str(yourdataframe) ?  I did not have
that issue just copying and pasting from your email and using the code
I showed above.

Cheers,

Josh

  
    
#
On Dec 9, 2010, at 12:44 PM, Sashi Challa wrote:

            
names(file.split) <- c("A1_toy_set.txt", "B1_toy_set_txt")
How are you proposing do do this "writing"?
Are you sure? Why would you think that?
#
On Dec 9, 2010, at 2:21 PM, David Winsemius wrote:

            
Those were not tabs after being processed by various portions of the  
various mail systems.
See if this works any better:

lapply(infile.split,function(x){
             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="/"),  
row.names=FALSE, quote=FALSE,sep="\t")
} )


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.
6 days later
#
Hi

I am still working on my large dataset (sample attached) that contains a series of binary variables (flags, yes/no) regarding affected brain areas ("Lica","LAtChor","LA1" ,etc).

I need to scan these columns, if value = Y for "Lxxx" set "LesionSide" to "L", if Y for "Rxxx" set to "R" and "B" if both.  There are >2500 records, so for-loops would be inefficient. Any suggestions?

Much obliged.

Best

Jon

Soli Deo Gloria

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: test.txt
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20101216/a1ded1e6/attachment.txt>
-------------- next part --------------


Jon Erik Ween, MD, MS
Scientist, Kunin-Lunenfeld Applied Research Unit 
Director, Stroke Clinic, Brain Health Clinic, Baycrest Centre
Assistant Professor, Dept. of Medicine, Div. of Neurology
    University of Toronto Faculty of Medicine

Kimel Family Building, 6th Floor, Room 644 
Baycrest Centre
3560 Bathurst Street 
Toronto, Ontario M6A 2E1
Canada 

Phone: 416-785-2500 x3648
Fax: 416-785-2484
Email: jween at klaru-baycrest.on.ca


Confidential: This communication and any attachment(s) may contain confidential or privileged information and is intended solely for the address(es) or the entity representing the recipient(s). If you have received this information in error, you are hereby advised to destroy the document and any attachment(s), make no copies of same and inform the sender immediately of the error. Any unauthorized use or disclosure of this information is strictly prohibited.
On 2010-12-09, at 2:21 PM, David Winsemius wrote: