[R] R column assignment fails for lists

Bert Gunter bgunter.4567 at gmail.com
Wed May 4 08:25:07 CEST 2016


This is actually a bit subtle -- you need to carefully read the Help
pages to see what's happening. Here's the explanation to the best of
my understanding (corrections happily accepted if I've got it wrong!).

First, let's simplify:

> z <- data.frame(a = 1:2, b = list(a = letters[1:2], b = letters[3:4]))

> z
  a b.a b.b
1 1   a   c
2 2   b   d

> ncol(z)
[1] 3


So we ended up with a 3 column data.frame where it seems we should
only have two, with the second being a list with 2 components.

But that's not how it works. ?data.frame says:

"... data.frame converts each of its arguments to a data frame by
calling as.data.frame(optional = TRUE) "

and ?as.data.frame says:

"If a list is supplied, each element is converted to a column in the
data frame."

Hence when as.data.frame() is called on the second column, a 2 element
list, it converts it into a 2 column data frame (of 2 rows each) thus
giving 3 columns in all in the data frame, yielding the error you saw.

Something like this is what also happens in your assignment, I assume.
Jim's solution assigned a 1 element list which yielded what you wanted
when as.data.frame converted it into a single column.

I think....

Cheers,
Bert




Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Tue, May 3, 2016 at 6:48 PM, David Winsemius <dwinsemius at comcast.net> wrote:
>
>> On May 3, 2016, at 4:13 PM, Yasir Suhail <yasir.suhail at gmail.com> wrote:
>>
>> Dear R developers and users,
>>
>> Consider the object :
>>
>>> a <- data.frame(a=c(1,2), b=c(2,3), c=c("a,b","c,d"), stringsAsFactors = F)
>>> a$c <- strsplit(a$c, ",")
>
> You are the one who should "consider the object". Look at what strsplit(a$c, ",") returns and then perhaps re-consider trying to assign it to a single column.
>
>>
>> Re-assignment works fine for columns 1 and 2, but fails for column 3. If a
>> is a valid object, the assignment should work.
>>
>>> a[,1] <- a[,1]
>>> a[,2] <- a[,2]
>>> a[,3] <- a[,3]
>> Warning message:
>> In `[<-.data.frame`(`*tmp*`, , 3, value = list(c("a", "b"), c("c",  :
>>  provided 2 variables to replace 1 variables
>>
>>       [[alternative HTML version deleted]]
>
> And please reconsider also the format of your postings.
>
>>
>> ______________________________________________
>> 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.
>
> David Winsemius
> Alameda, CA, USA
>
> ______________________________________________
> 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