[R] list as object in dataframe

jim holtman jholtman at gmail.com
Fri Mar 28 00:41:00 CET 2008


df$x is list so you have to use this type of assignment

> df$x[[1]] <- c('a','b')
> df
  y    x
1 1 a, b
2 1 f, c
3 1    a
> df$x[[3]] <- letters
> df
  y            x
1 1         a, b
2 1         f, c
3 1 a, b, c,....
> str(df)
'data.frame':   3 obs. of  2 variables:
 $ y: num  1 1 1
 $ x:List of 3
  ..$ : chr  "a" "b"
  ..$ : chr  "f" "c"
  ..$ : chr  "a" "b" "c" "d" ...
  ..- attr(*, "class")= chr "AsIs"
>



On Thu, Mar 27, 2008 at 4:27 PM, Daniel E. Bunker <deb37 at columbia.edu> wrote:
> Hi All,
>
> I need to place lists or vectors within dataframes as single
> elements.  However when I try this:
>
> df=data.frame(y=1, x=I(list(c("a","b"), c("f","c"), c("a"))))
> df
>
> df[1,'x']=I(c("a","d"))
>
> I get this error, even though I am using I():
>
> Error in `[<-.data.frame`(`*tmp*`, 1, "x", value = c("a", "d")) :
> replacement has 2 rows, data has 1
>
> Note that this behavior does not match that described here <http://
> finzi.psych.upenn.edu/R/Rhelp02a/archive/37297.html>  in this post
> from 2004.
>
> Can someone please point me towards the right way to do this?
>
> Thanks!!
>
> Dan
>
>
>
> $platform
> [1] "i386-apple-darwin8.10.1"
>
> $arch
> [1] "i386"
>
> $os
> [1] "darwin8.10.1"
>
> $system
> [1] "i386, darwin8.10.1"
>
> $status
> [1] ""
>
> $major
> [1] "2"
>
> $minor
> [1] "6.2"
>
> $year
> [1] "2008"
>
> $month
> [1] "02"
>
> $day
> [1] "08"
>
> $`svn rev`
> [1] "44383"
>
> $language
> [1] "R"
>
> $version.string
> [1] "R version 2.6.2 (2008-02-08)"
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list