[R] use of apply in a data frame on a row by row basis

From: Julien Laugel <roolio4news_at_gmail.com>
Date: Thu 06 Jul 2006 - 22:30:53 EST


Hello all,

I'm trying to use the apply function on a data frame, by applying a function that takes a one row data.frame as argument .

Here's the example :
myfun = function(x) paste(x$f1 , x$f2)
df = data.frame(f1 = c(1,4,10),f2 = "hello")

apply(df,1,myfun) ==> Does not work (I get "character(0)" )

Though : myfun(df[1,]) works,
and myfun(df) works as well.

So if myfun(df) works, that's fine!
BUT, if the output of the function a bit more complex, it does not work : In this case I'm using a timeSeries object (from Rmetrics)

library(fMultivar)
timerange = timeSequence(from = "2001-04-11", length.out = 3,by = "weeks", format = "%Y-%m-%d", FinCenter = "GMT")
myfun2 = function(x) timeSeries(rnorm(3),timerange)

In this case, myfun2(df) returns only the result of the 1st row! BUT
apply(df,1,myfun2) in this case, does work

But I haven't used the "$" notation to access one field Now if I try (and that is the cas that bugs me currently) :

myfun3 = function(x) timeSeries(rep(x$f1,3),timerange)

myfun3(df)
Error in "rownames<-"(`*tmp*`, value = c("2001-04-11 02:00:00", "2001-04-18 02:00:00", :
length of 'dimnames' [1] is not equal to range of the array (sorry for the translation : I'm using a french version of R) Though
> myfun3(df[1,])

TS.1

2001-04-11 02:00:00 1
2001-04-18 02:00:00 1
2001-04-25 02:00:00 1

works well
BUT :
apply(df,1,myfun3)
yields :

Error in array(x, c(length(x), 1), if (!is.null(names(x))) list(names(x), : trying to change an attribute in NULL

So as a result I'm verry annoyed, and I don't see the logic behind. How can I do to make a apply-like function on the latter function? (myfun3)

Many thanks in advance

Regards,

Roolio

        [[alternative HTML version deleted]]



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Jul 06 22:35:18 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 07 Jul 2006 - 00:14:45 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.