[R] How to intantiate a list of data.frames?

Joshua Wiley jwiley.psych at gmail.com
Tue May 24 23:30:56 CEST 2011


Hi Rui,

Please look at the documentation for ?write.csv

I do not have oilDF, but my guess is that you make the object, "ds"
fine, but then you are trying to pass a list to write.csv which works
on matrices or data frames (or attempts to coerce to such).  The
easiest answer is probably to write each element of "ds" (that is,
each data frame) to a separate file.

Cheers,

Josh

On Sun, May 22, 2011 at 12:11 PM, Rui Maximo <ruimaximo at hotmail.com> wrote:
> I will post the whole function, but I believe the problem is in the 3th
> part.
> The issue is that oilDF has different number of rows than oilDF2.
>
> Thank you,
> Rui
>
> myScan <- function(dirPath, num)
> {
> #dirPath is the name of the directory where we want to apply the function.
> It should be called from the immediate above level without the last 3
> characters. For example dirPath="oil_0"
> #num is the mussel number
>
>     #Heart rate
>     startPath=getwd()
>     workPath=paste(startPath,"/", dirPath,"_HR", sep="")
>     setwd(workPath)
>     temp=dir()
>     d=sort(temp)
>     oilDF=read.table (d[1], header=TRUE)
>     oilDF=data.frame(oilDF[,1], oilDF[,2], oilDF[,num+2])
>     for(i in 2:length(d))
>     {
>         temp <- read.table(d[i], header=TRUE)
>         temp=data.frame(temp[,1], temp[,2], temp[,num+2])
>         colnames(temp) <- colnames(oilDF)
>         oilDF=rbind(oilDF,temp)
>     }
>     setwd(startPath)
>
>     #Valve Gape
>     workPath=paste(startPath,"/", dirPath,"_VG", sep="")
>     setwd(workPath)
>     temp=dir()
>     d=sort(temp)
>     oilDF2=read.table (d[1], header=FALSE)
>     oilDF2=data.frame(oilDF2[,1],oilDF2[,2],oilDF2[,num+3])
>     for(i in 2:length(d))
>     {
>         temp <- read.table(d[i], header=FALSE)
>         temp=data.frame(temp[,1], temp[,2], temp[,num+3])
>         colnames(temp) <- colnames(oilDF2)
>         oilDF2=rbind(oilDF2,temp)
>     }
>
>     #Pack both signals in a vector of dataframes for each Mussel.
>     ds <- vector("list", 2)
>     timeHR = as.numeric(strptime(paste(oilDF[,1],oilDF[,2]), "%m/%d/%y
> %H:%M:%OS"))
>     timeVG = as.numeric(strptime(paste(oilDF2[,1],oilDF2[,2]), "%d/%m/%y
> %H:%M:%OS"))
>     ds[[1]] <- data.frame(timeHR,oilDF[,3])
>     ds[[2]] <- data.frame(timeVG,oilDF2[,3])
>     write.csv(ds,paste(startPath, "/", "mussel_", i, dirPath, ".csv",
> sep=""))
>     return(ds)
> }
>
>> Date: Sun, 22 May 2011 11:33:38 -0700
>> Subject: Re: [R] How to intantiate a list of data.frames?
>> From: jwiley.psych at gmail.com
>> To: ruimaximo at hotmail.com
>> CC: r-help at r-project.org
>>
>> Hi Rui,
>>
>> data frames must have the same number of rows, but two different data
>> frames stored within a list do not need to have the same number of
>> rows. Can you please post the code that is giving the error?
>>
>> Josh
>>
>> On Sun, May 22, 2011 at 9:41 AM, Rui Maximo <ruimaximo at hotmail.com> wrote:
>> > Hi Josh,
>> >
>> > Sorry, your examples have equal number of rows in both df and df2.
>> > In my situation they haven't.
>> > Strangely, your solution have worked only when I am copy post the code
>> > into
>> > the command line.
>> > If I use the code inside of a function I get an error at:
>> > return(ds)
>> > ERROR: arguments imply differing number of rows
>> >
>> > Thanks,
>> > Rui
>> >
>> >> Date: Sat, 21 May 2011 11:46:05 -0700
>> >> Subject: Re: [R] How to intantiate a list of data.frames?
>> >> From: jwiley.psych at gmail.com
>> >> To: ruimaximo at hotmail.com
>> >> CC: r-help at r-project.org
>> >>
>> >> Hi Rui,
>> >>
>> >> Here is one option:
>> >>
>> >> ds <- vector("list", 6)
>> >> for(i in 1:6) ds[[i]] <- list(df = mtcars[, c(i, i + 2)], df2 =
>> >> mtcars[, c(i, i + 2)] + 10)
>> >>
>> >> another could be:
>> >>
>> >> altds <- lapply(1:6, function(x) {
>> >> list(df = mtcars[, c(x, x + 2)], df2 = mtcars[, c(x, x + 2)] + 10)
>> >> })
>> >>
>> >> all.equal(ds, altds)
>> >>
>> >> For some documentation, see
>> >>
>> >> ?vector
>> >> ?lapply
>> >>
>> >> Cheers,
>> >>
>> >> Josh
>> >>
>> >> On Sat, May 21, 2011 at 10:47 AM, Rui Maximo <ruimaximo at hotmail.com>
>> >> wrote:
>> >> >
>> >> > Hello,
>> >> >
>> >> >  I am newbie to R and I want to do this:
>> >> >
>> >> > for(i in 1:6)
>> >> > {
>> >> >        ds[i] <- list(df=data.frame(oilDF[,1],oilDF[,i+2]),
>> >> > df2=data.frame(oilDF2[,1],oilDF2[,i+2]))
>> >> > }
>> >> >
>> >> > #oilDF and oilDF2 are 2 data frames with several columns. They have
>> >> > different number of rows
>> >> >
>> >> > #I want to have for example ds[1]$df, ds[1]$df2 with the respective
>> >> > data.frames.
>> >> > #How can I instantiate a list of data.frames pairs with different
>> >> > number
>> >> > of rows?
>> >> >
>> >> > Thank you,
>> >> > Rui
>> >> >
>> >> >        [[alternative HTML version deleted]]
>> >> >
>> >> > ______________________________________________
>> >> > R-help at r-project.org mailing list
>> >> > 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.
>> >> >
>> >>
>> >>
>> >>
>> >> --
>> >> Joshua Wiley
>> >> Ph.D. Student, Health Psychology
>> >> University of California, Los Angeles
>> >> http://www.joshuawiley.com/
>> >
>>
>>
>>
>> --
>> Joshua Wiley
>> Ph.D. Student, Health Psychology
>> University of California, Los Angeles
>> http://www.joshuawiley.com/
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list