[R] combining numeric vector and column in a data frame

Simon Knapp sleepingwell at gmail.com
Wed Jul 11 01:01:26 CEST 2012


combined <- data.frame(mnv=my.numeric.vec[df1$fileName], type=df1$type)
sorted <- combined[order(rownames(combined)),]




On Wed, Jul 11, 2012 at 8:38 AM, Adrian Johnson
<oriolebaltimore at gmail.com> wrote:
> Hi:
> I am trying to map column names of a matrix to another data frame and
> get values in a different column of data frame
>
>
> I have a matrix m.
>
>> my.numeric vec <- m[1,]
>
>> my.numeric.vec
> a     b     c    d   e    f
> 2     1     4    9   10  3
>
>
>
> ##  my data frame = df1
>
>> df1
>
> fileName    type   status
> b               N       alive
> a               T        dead
> d               N       alive
> c               T       dead
> f               N        alive
> e              T        dead
>
>
> I want to combine as.numeric.vec and df1 and create j with correct
> filename and type and numeric value
>
>>j
>
> my.numeric.vec    type
> a 2   T
> b 1  N
> c 4  T
> d 9  N
> e 10 T
> f 3  N
>
> How can I combine my.numeric.vec and df1$type
>
> when I try:
>
> df1[df1$fileName %in% names(my.numeric.vec),2]
>
> I get wrong answer.
>
>
> thanks in advance.
>
> Adrian
>
> ______________________________________________
> 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