Skip to content

R column assignment fails for lists

6 messages · Yasir Suhail, Jim Lemon, David Winsemius +1 more

#
Dear R developers and users,

Consider the object :
Re-assignment works fine for columns 1 and 2, but fails for column 3. If a
is a valid object, the assignment should work.
Warning message:
In `[<-.data.frame`(`*tmp*`, , 3, value = list(c("a", "b"), c("c",  :
  provided 2 variables to replace 1 variables
#
Hi Yasil,
If you look at what happens to a[,3] after the "strsplit" it is easy:
[1] "a,b" "c,d"

Here a[,3] is two strings

a$c <- strsplit(a$c, ",")
[[1]]
[1] "a" "b"

[[2]]
[1] "c" "d"

Now a[,3] is a two element list. What R probably did was to take the
first component of a[,3] to replace the existing two values. Now if
you don't try to fool R:
[[1]]
[1] "a" "b"

[[2]]
[1] "c" "d"

Jim
On Wed, May 4, 2016 at 9:13 AM, Yasir Suhail <yasir.suhail at gmail.com> wrote:
#
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.
And please reconsider also the format of your postings.
David Winsemius
Alameda, CA, USA
#
Dear Jim and David,

Thank you very much for your reply. I guess my question is whether it is
legal to store vectors in the elements of a data.frame. These are useful
for storing things like neighbors of a graph vertex, orthologs of a gene
etc. I have been using data frames of this sort and they are very useful,
so I am hoping this is a feature, rather than a bug. The weird code in my
email to form a was just one quick way of making such an object for a toy
example. These sort of data frames were doing fine for a number of analysis
pipelines until I used a function on them that had code of the sort
df[, j] <- sapply(df[, j], fun)

The interesting thing is that while
a[,3] <- a[,3] changes the object and gives the warning
a$c <- a$c works "correctly". leaving a to what it was and doesn't give a
warning.

If elements of a data frame are supposed to be able to store vectors, then
shouldn't
a[,3] <- a[,3] work?

Best regards,
yasir


On Tue, May 3, 2016 at 9:48 PM, David Winsemius <dwinsemius at comcast.net>
wrote:

  
  
#
Try working with a 3 row dataframe. Then your misconceptions about how this proposed assignment will be more prominent because the dimensions would be wrong.

Here's an alternate approach:
[[1]]
[1] "a" "b"

[[2]]
[1] "c" "d"

[[3]]
[1] "e" "f"
[,1] [,2]
[1,] "a"  "b" 
[2,] "c"  "d" 
[3,] "e"  "f"
'data.frame':	3 obs. of  4 variables:
 $ a: num  1 2 3
 $ b: num  2 3 4
 $ 1: chr  "a" "c" "e"
 $ 2: chr  "b" "d" "f"
#
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:
a b.a b.b
1 1   a   c
2 2   b   d
[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: