[R] apply --> data.frame

Rui Barradas ruipbarradas at sapo.pt
Fri Aug 31 13:00:32 CEST 2012


Hello,

Em 31-08-2012 11:35, Martin Maechler escreveu:
> Hi Rui,
>
> I think when people are asking how to do such basic things in R,
> the answer should *not* be to use a (non-base / -recommended)
> package...

You're right, that's why I've called it an alternative.

>
> but then, you may have really wanted to advertize plyr,
> so never mind.

It' usefull to know that those extra *ply functions are available.

Anyway, my vote goes to Bill Dunlap's read.table solution. I would never 
have thought of that one.

Rui Barradas
>
> Martin
>
>>>>>> "RB" == Rui Barradas <ruipbarradas at sapo.pt>
>>>>>>      on Thu, 30 Aug 2012 18:57:34 +0100 writes:
>      RB> Hello,
>
>      RB> Yet another alternative.
>
>
>      RB> library(plyr)
>      RB> dfr <- ldply(strsplit(c("a,1", "b,2", "c,3"), ","), identity)
>      RB> str(dfr)
>
>      RB> #dfr$V2 <- as.numeric(dfr$V2)
>
>      RB> So, if the op was about conversion to df, the answer is yes.
>
>      RB> Rui Barradas
>
>      RB> Em 30-08-2012 18:14, David Winsemius escreveu:
>      >>
>      >> On Aug 30, 2012, at 9:44 AM, Sam Steingold wrote:
>      >>
>      >>>> * Sam Steingold <fqf at tah.bet> [2012-08-30 08:56:17 -0400]:
>      >>>>
>      >>>> Is there a way for an apply-type function to return a data frame?
>      >>>> the closest thing I think of is
>      >>>>
>      >>>> foo <- as.data.frame(t(sapply(...)))
>      >>>> names(foo) <- c(....)
>      >>>
>      >>> alas, this has a problem of creating a "homogeneous" data frame, i.e.,
>      >>> all the columns are numbers or characters, because the function passed
>      >>> to sapply returns c(....) and
>      >>>> c(1,2,"a")
>      >>> [1] "1" "2" "a"
>      >>>
>      >>> e.g.,
>      >>> as.data.frame(t(sapply(c("a,1","b,2","c,3"),function (n)
>      >>> strsplit(n,",")[[1]])))
>      >>> V1 V2
>      >>> a,1  a  1
>      >>> b,2  b  2
>      >>> c,3  c  3
>      >>>
>      >>> 'data.frame':    3 obs. of  2 variables:
>      >>> $ V1: Factor w/ 3 levels "a","b","c": 1 2 3
>      >>> ..- attr(*, "names")= chr  "a,1" "b,2" "c,3"
>      >>> $ V2: Factor w/ 3 levels "1","2","3": 1 2 3
>      >>> ..- attr(*, "names")= chr  "a,1" "b,2" "c,3"
>      >>>
>      >>> I wanted the V1 column to be a string, and V2 to be a number.
>      >>> (I know stringsAsFactors=FALSE would replace factors with strings, but I
>      >>> need a string and a number)
>      >>>
>      >>> I could, of course, do ret$V2 <- as.numeric(ret$V2) but this would mean
>      >>> a double conversion: from number to string first (by c()) and then back.
>      >>
>      >> It is starting as a 'string' ('character' in R parlance) so you will
>      >> need to coerce it to "numeric" at some point:
>      >>
>      >> Consider this alternate route:
>      >>
>      >> > do.call(rbind, strsplit(c("a,1","b,2","c,3"), ",") )
>      >> [,1] [,2]
>      >> [1,] "a"  "1"
>      >> [2,] "b"  "2"
>      >> [3,] "c"  "3"
>      >> > as.data.frame( do.call(rbind, strsplit(c("a,1","b,2","c,3"), ",") ) )
>      >> V1 V2
>      >> 1  a  1
>      >> 2  b  2
>      >> 3  c  3
>      >>
>      >> > str( as.data.frame( do.call(rbind, strsplit(c("a,1","b,2","c,3"),
>      >> ",") ) , stringsAsFactors=FALSE) )
>      >> 'data.frame':    3 obs. of  2 variables:
>      >> $ V1: chr  "a" "b" "c"
>      >> $ V2: chr  "1" "2" "3"
>      >>
>
>      RB> ______________________________________________
>      RB> R-help at r-project.org mailing list
>      RB> https://stat.ethz.ch/mailman/listinfo/r-help
>      RB> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>      RB> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list