[R] R column assignment fails for lists

Yasir Suhail yasir.suhail at gmail.com
Wed May 4 05:58:26 CEST 2016


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:

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

	[[alternative HTML version deleted]]



More information about the R-help mailing list