[R] Help batch saving elements of a list into unique files

Christian T Stackhouse (Campus) ctstackh at uab.edu
Wed Mar 23 21:39:20 CET 2016


Jim, 

I wanted to thank you for your help and let you know I came up with a very simple solution to the header issue. I made a vector of the original header names before the split and fed that to names in your for loop:

h1 <- colnames(affymatrix)

for(affdf in 1:length(out)) {
  names(out[[affdf]])<-h1[2:119]
  write.csv(out[[affdf]],file=paste("affymetrix",affdf,".txt",sep=""))
}

Thank you for showing me how to save these files iteratively. I'm generating hundreds if not more than a thousand small files for batch operation.. Not something I wanted to tackle manually.

Sincerely,

Christian T. Stackhouse | Graduate Student
GBS Neuroscience Theme
Department of Neurosurgery
Department of Radiation Oncology
UAB | The University of Alabama at Birmingham
Hazelrig-Salter Radiation Oncology Center | 1700 6th Ave S | Birmingham, AL 35233
M: 919.724.6890 | ctstackh at uab.edu | cstackhouse at uabmc.edu | ctstackh at gmail.com

uab.edu
Knowledge that will change your world


________________________________________
From: Jim Lemon <drjimlemon at gmail.com>
Sent: Tuesday, March 22, 2016 8:46 PM
To: Christian T Stackhouse (Campus)
Cc: r-help at r-project.org
Subject: Re: [R] Help batch saving elements of a list into unique files

Okay. Got some lunch, I can think about this with both halves of the brain.

drop_token1<-function(x) {
 return(paste(x[2:length(x)],sep="",collapse="."))
}
affnames<-c("X0.Classical.10.11.1_.HuEx.1_0.st.v2..CEL",
 "X1.Classical.10.11.1_.HuEx.1_0.st.v2..CEL")
affnames.split<-strsplit(affnames,"[.]")
lapply(affnames.split,drop_token1)
[[1]]
[1] "Classical.10.11.1_.HuEx.1_0.st.v2..CEL"

[[2]]
[1] "Classical.10.11.1_.HuEx.1_0.st.v2..CEL"

This what I get with a toy example. So, I think that:

for(affdf in 1:length(out)) {
names(out[[affdf]])<-lapply(strsplit(names(out[[affdf]]),"[.]),drop_token1)
write.csv(out[[affdf]],file=paste("affymetrix",affdf,".txt",sep=""))
}

should work.

Jim


On Wed, Mar 23, 2016 at 11:16 AM, Christian T Stackhouse (Campus)
<ctstackh at uab.edu> wrote:
> I re ran it and this is what I got: 11.1_.HuEx.1_0.st.v2..CEL
> Should be: 10.11.1_.HuEx.1_0.st.v2..CEL
>
> Christian T. Stackhouse | Graduate Student
> GBS Neuroscience Theme
> Department of Neurosurgery
> Department of Radiation Oncology
> UAB | The University of Alabama at Birmingham
> Hazelrig-Salter Radiation Oncology Center | 1700 6th Ave S | Birmingham, AL 35233
> M: 919.724.6890 | ctstackh at uab.edu | cstackhouse at uabmc.edu | ctstackh at gmail.com
>
> uab.edu
> Knowledge that will change your world
>
>
> ________________________________________
> From: Jim Lemon <drjimlemon at gmail.com>
> Sent: Tuesday, March 22, 2016 6:24 PM
> To: Christian T Stackhouse (Campus)
> Cc: r-help at r-project.org
> Subject: Re: [R] Help batch saving elements of a list into unique files
>
> Transcription. I forgot the "collapse" argument when I wrote the email:
>
> drop_token1<-function(x) {
>  return(paste(x[2:length(x)],sep="",collapse="."))
> }
>
> Jim
>
>
> On Wed, Mar 23, 2016 at 10:14 AM, Christian T Stackhouse (Campus)
> <ctstackh at uab.edu> wrote:
>> Very close! The header now looks like this: c("10", "11", "1_", "HuEx", "1_0", "st", "v2", "", "CEL")
>>  For some reason, it's not concatenating.
>>
>> Best,
>>
>> Christian T. Stackhouse | Graduate Student
>> GBS Neuroscience Theme
>> Department of Neurosurgery
>> Department of Radiation Oncology
>> UAB | The University of Alabama at Birmingham
>> Hazelrig-Salter Radiation Oncology Center | 1700 6th Ave S | Birmingham, AL 35233
>> M: 919.724.6890 | ctstackh at uab.edu | cstackhouse at uabmc.edu | ctstackh at gmail.com
>>
>> uab.edu
>> Knowledge that will change your world
>>
>>
>> ________________________________________
>> From: Jim Lemon <drjimlemon at gmail.com>
>> Sent: Tuesday, March 22, 2016 6:02 PM
>> To: Christian T Stackhouse (Campus)
>> Cc: r-help at r-project.org
>> Subject: Re: [R] Help batch saving elements of a list into unique files
>>
>> I think it's the "unlist". I can only test this with one set of made
>> up names at a time.
>>
>> names(out[[affdf]])<-
>>  lapply(strsplit(names(out[[affdf]]),"[.]"),drop_token1)
>>
>> Jim
>>
>>
>> On Wed, Mar 23, 2016 at 9:57 AM, Christian T Stackhouse (Campus)
>> <ctstackh at uab.edu> wrote:
>>> This is what I ran:
>>>
>>>> drop_token1<-function(x) {
>>> +   return(paste(x[2:length(x)],sep="."))
>>> + }
>>>> for(affdf in 1:length(out)) {
>>> +   names(out[[affdf]])<-lapply(unlist(strsplit(names(out[[affdf]]),"[.]")),drop_token1)
>>> +   write.csv(out[[affdf]],file=paste("affymetrix",affdf,".txt",sep=""))
>>> + }
>>> Error in names(out[[affdf]]) <- lapply(unlist(strsplit(names(out[[affdf]]),  :
>>>   'names' attribute [1148] must be the same length as the vector [118]
>>>>
>>>
>>> This is what the header was before:
>>>
>>> X0.Classical.10.11.1_.HuEx.1_0.st.v2..CEL
>>>
>>> There was no output due to the error.
>>>
>>> Christian T. Stackhouse | Graduate Student
>>> GBS Neuroscience Theme
>>> Department of Neurosurgery
>>> Department of Radiation Oncology
>>> UAB | The University of Alabama at Birmingham
>>> Hazelrig-Salter Radiation Oncology Center | 1700 6th Ave S | Birmingham, AL 35233
>>> M: 919.724.6890 | ctstackh at uab.edu | cstackhouse at uabmc.edu | ctstackh at gmail.com
>>>
>>> uab.edu
>>> Knowledge that will change your world
>>>
>>>
>>> ________________________________________
>>> From: Jim Lemon <drjimlemon at gmail.com>
>>> Sent: Tuesday, March 22, 2016 5:46 PM
>>> To: Christian T Stackhouse (Campus)
>>> Cc: r-help at r-project.org
>>> Subject: Re: [R] Help batch saving elements of a list into unique files
>>>
>>> Sorry, should be:
>>>
>>> names(out[[affdf]])<-
>>>  lapply(unlist(strsplit(names(out[[affdf]]),"[.]")),drop_token1)
>>>
>>> Jim
>>>
>>>
>>> On Wed, Mar 23, 2016 at 9:43 AM, Christian T Stackhouse (Campus)
>>> <ctstackh at uab.edu> wrote:
>>>> Thank you, Jim. I got this error returned:
>>>>
>>>> Error in strsplit(names(out[[affdf]])) :
>>>>   argument "split" is missing, with no default
>>>>
>>>> Christian T. Stackhouse | Graduate Student
>>>> GBS Neuroscience Theme
>>>> Department of Neurosurgery
>>>> Department of Radiation Oncology
>>>> UAB | The University of Alabama at Birmingham
>>>> Hazelrig-Salter Radiation Oncology Center | 1700 6th Ave S | Birmingham, AL 35233
>>>> M: 919.724.6890 | ctstackh at uab.edu | cstackhouse at uabmc.edu | ctstackh at gmail.com
>>>>
>>>> uab.edu
>>>> Knowledge that will change your world
>>>>
>>>>
>>>> ________________________________________
>>>> From: Jim Lemon <drjimlemon at gmail.com>
>>>> Sent: Tuesday, March 22, 2016 5:39 PM
>>>> To: Christian T Stackhouse (Campus)
>>>> Cc: r-help at r-project.org
>>>> Subject: Re: [R] Help batch saving elements of a list into unique files
>>>>
>>>> Okay, I just snipped off the first token in the header labels assuming
>>>> that there would be no more periods. Try this:
>>>>
>>>> drop_token1<-function(x) {
>>>>  return(paste(x[2:length(x)],sep="."))
>>>> }
>>>> for(affdf in 1:length(out)) {
>>>> names(out[[affdf]])<-lapply(unlist(strsplit(names(out[[affdf]]))),drop_token1)
>>>> write.csv(out[[affdf]],file=paste("affymetrix",affdf,".txt",sep=""))
>>>> }
>>>>
>>>> Jim
>>>>
>>>> On Wed, Mar 23, 2016 at 9:13 AM, Christian T Stackhouse (Campus)
>>>> <ctstackh at uab.edu> wrote:
>>>>> Jim,
>>>>>
>>>>> It worked! It wrote out the files, but unfortunately, it didn't work for the file headers. I should have mentioned this is what the headers look like: X0.Classical.10.11.1_.HuEx.1_0.st.v2..CEL
>>>>> After running your script, that header changes to: 10. I'd just like to remove the "X0." prefix or in the case of file 189 the "X188." prefix leaving: Classical.10.11.1_.HuEx.1_0.st.v2..CEL
>>>>>
>>>>> Thank you so much for your help! I'll try playing with it myself, but if you have any further insights they would be greatly appreciated!
>>>>>
>>>>> Best,
>>>>>
>>>>> Christian T. Stackhouse | Graduate Student
>>>>> GBS Neuroscience Theme
>>>>> Department of Neurosurgery
>>>>> Department of Radiation Oncology
>>>>> UAB | The University of Alabama at Birmingham
>>>>> Hazelrig-Salter Radiation Oncology Center | 1700 6th Ave S | Birmingham, AL 35233
>>>>> M: 919.724.6890 | ctstackh at uab.edu | cstackhouse at uabmc.edu | ctstackh at gmail.com
>>>>>
>>>>> uab.edu
>>>>> Knowledge that will change your world
>>>>>
>>>>>
>>>>> ________________________________________
>>>>> From: Jim Lemon <drjimlemon at gmail.com>
>>>>> Sent: Tuesday, March 22, 2016 4:48 PM
>>>>> To: Christian T Stackhouse (Campus)
>>>>> Cc: r-help at r-project.org
>>>>> Subject: Re: [R] Help batch saving elements of a list into unique files
>>>>>
>>>>> Hi Christian,
>>>>> This untested script might get you going (assuming you want a CSV format):
>>>>>
>>>>> for(affdf in 1:length(out)) {
>>>>>  names(out[[affdf]])<-lapply(strsplit(names(out[[affdf]]),"[.]"),"[",2)
>>>>>  write.csv(out[[affdf]],file=paste("affymetrix",affdf,".txt",sep=""))
>>>>> }
>>>>>
>>>>> Jim
>>>>>
>>>>>
>>>>> On Wed, Mar 23, 2016 at 6:32 AM, Christian T Stackhouse (Campus)
>>>>> <ctstackh at uab.edu> wrote:
>>>>>> Hello!
>>>>>>
>>>>>>
>>>>>> The overall goal I have is taking a large data frame and splitting it into several smaller data frames (preserving column headers) which I can save as txt files to feed into my APACHE ANY23 server for conversion into RDF.
>>>>>>
>>>>>>
>>>>>> This is what I call to split up the original file:
>>>>>>
>>>>>>
>>>>>> out <- split(affymetrix, (seq(nrow(affymetrix))-1) %/% 140)
>>>>>>
>>>>>>
>>>>>> I have a list (out) of length 187 for which each element is a dataframe. I want to iteratively save each data frame as a separate tab file with a naming structure such as: affymetrix1.txt, affymetrix2.txt, ... affymetrix187.txt
>>>>>>
>>>>>>
>>>>>> Before that, I need to modify the headers to remove a prefix "X0. , X1., ... X187." that was introduced during my original splitting. I need to remove all characters before and including the first "."
>>>>>>
>>>>>>
>>>>>> If anyone has a better way of doing this, please let me know. Otherwise, help with how to perform batch editing of the headers and batch saving of the files would be greatly appreciated!
>>>>>>
>>>>>>
>>>>>> Best,
>>>>>>
>>>>>> Christian T. Stackhouse | Graduate Student
>>>>>> GBS Neuroscience Theme
>>>>>> Department of Neurosurgery
>>>>>> Department of Radiation Oncology
>>>>>> UAB | The University of Alabama at Birmingham
>>>>>> Hazelrig-Salter Radiation Oncology Center | 1700 6th Ave S | Birmingham, AL 35233
>>>>>> M: 919.724.6890 | ctstackh at uab.edu | cstackhouse at uabmc.edu | ctstackh at gmail.com
>>>>>>
>>>>>> uab.edu<http://uab.edu/>
>>>>>> Knowledge that will change your world
>>>>>>
>>>>>>
>>>>>>         [[alternative HTML version deleted]]
>>>>>>
>>>>>> ______________________________________________
>>>>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>>>>> and provide commented, minimal, self-contained, reproducible code.


More information about the R-help mailing list