[R] Passing vectors through a dataframe

arun smartpink111 at yahoo.com
Fri Feb 7 19:24:27 CET 2014


Hi,
Try:

  mylist <- list(F4087=F4087, F4287=F4287)
#or
mylist <- list(F4087, F4287)
 names(mylist) <- dxnames
 mylist[[maxr2]]
#[1] 3423  543 6768  867  945 3456 5634 6543  345

mylist[dxnames]
$F4087
[1]  234 1234   65   34  624  345

$F4287
[1] 3423  543 6768  867  945 3456 5634 6543  345

A.K.


On Friday, February 7, 2014 11:42 AM, Jeremy Clark <jeremyclarkmel at gmail.com> wrote:
## Many thanks ! - the list idea almost works - but I can't find the
indexing ## needed to extract an element from the list using an object
created from ## the dataframe.

## maxr2[[1]] doesn't seem to work despite being the correct character

## string.



## I've now added a list to the example (which can be copied and pasted ##
into R). Thanks again in advance.



## Example coding:



library(gdata)

F4087 <- c(234,1234,65,34,624,345)

F4287 <- c(3423,543,6768,867,945,3456,5634,6543,345)

mylist <- list(F4087, F4287)



value <- c(0.1, 0.3)

dxnames <- c("F4087", "F4287")

df <- data.frame(value, dxnames)



## find row with max value (which is 0.3, "F4287")

df.agg <- aggregate(value ~ value, df, max)

r.max <- merge(df.agg, df)

r.max2 <- drop.levels(r.max)



## now extract one element from this row ie. "F4287"

maxr <- r.max2["dxnames"]

maxr[[1]]



## now try to put it in form to be used for indexing mylist

maxr2 <- toString(maxr[[1]])

maxr2

maxr2[[1]]



## try to extract correct vector (ie. F4287):

maxtrundx <- mylist[[maxr2[[1]]]]       ## incorrect indexing ??

maxtrundx

maxtrundx <- mylist[[maxr[[1]]]]       ## incorrect - gives wrong vector

maxtrundx





## The question is - how can I select the correct vector from the list
(ie.F4287) using an object extracted from the dataframe (eg. maxr or maxr2)
??








On Thu, Feb 6, 2014 at 6:20 PM, Duncan Murdoch <murdoch.duncan at gmail.com>wrote:

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


-- 


MEDIMEL,
www.plastic-surgery-poland.co.uk
www.chirurgia-szczecin.pl
Jeremy Clark. Address: ul. Klonowica 45/2, 71-249 Szczecin, Poland.

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





More information about the R-help mailing list