[R] Passing vectors through a dataframe

Duncan Murdoch murdoch.duncan at gmail.com
Thu Feb 6 18:20:20 CET 2014


On 06/02/2014 10:00 AM, Jeremy Clark wrote:
> Dear All,
>
> I would like to be able to associate a list of vectors (one vector of
> which is to be called later) with some other character and numeric
> data. I've tried two methods:
>
> 1) I can put the vector names in quotes into the dataframe, and then
> extract the vector name - but this is just a character string - which
> I would then like to use to re-express the original vector, which was
> defined earlier.
> 2) If I try to put the vectors directly into the dataframe it's not
> accepted because the vectors have different lengths (and in any case
> it is the name of the vector which needs to be associated with the
> other data, not particular values).

Just use a list, not a dataframe.  They can hold any objects, and can 
hold them by name if necessary.

I haven't followed through your example, but the start of it could be 
done like this:

mylist <- list(F4087 = c(234,1234,65,34,624,345),
                        F4187 = 
c(234,542,4523,56345,233,5654,776,4788,78768,456,8756),
                        F4287 = c(3423,543,6768,867,945,3456,5634,6543,345))

and then you can extract F4287 as mylist[["F4287"]].

Duncan Murdoch

>
> Probably I'm missing something basic ? Many thanks in advance (!)
>
> Example coding showing how far I've got is as follows:
>
> library(gdata)
>
> F4087 <- c(234,1234,65,34,624,345)
> F4187 <- c(234,542,4523,56345,233,5654,776,4788,78768,456,8756)
> F4287 <- c(3423,543,6768,867,945,3456,5634,6543,345)
>
> rsquaredtrun <- c("R4087", "R4187", "R4287")
> rsquaredtrun
> rsquaredvalues <- c(0.1, 0.2, 0.3)
> rsquaredvalues
> dxtrunlist <- c("F4087", "F4187", "F4287")
> dxtrunlist
>
> rsquareddf <- data.frame(rsquaredtrun, rsquaredvalues, dxtrunlist)
> rsquareddf
>
> df.agg <- aggregate(rsquaredvalues ~ rsquaredvalues, rsquareddf, max)
> df.agg
> rsquared.max <- merge(df.agg, rsquareddf)
> rsquared.max
> rsquared.max2 <- drop.levels(rsquared.max)
> rsquared.max2
> maxr <- rsquared.max2["dxtrunlist"]
> maxr
> maxrr <- as.matrix(maxr)
> maxrr
> maxrrr <- as.vector(maxrr)
> maxrrr
>
> The question is - how can I resurrect the vector F4287 from maxrrr  ??
>
> ______________________________________________
> 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.




More information about the R-help mailing list