[R] apply --> data.frame

Rui Barradas ruipbarradas at sapo.pt
Thu Aug 30 19:57:34 CEST 2012


Hello,

Yet another alternative.


library(plyr)
dfr <- ldply(strsplit(c("a,1", "b,2", "c,3"), ","), identity)
str(dfr)

#dfr$V2 <- as.numeric(dfr$V2)

So, if the op was about conversion to df, the answer is yes.

Rui Barradas

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"
>




More information about the R-help mailing list