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

2012-07-10 Thread Rui Barradas

Hello,

Here are two ways of doing it.


df1 <- read.table(text="
fileNametype   status
b   N   alive
a   Tdead
d   N   alive
c   T   dead
f   Nalive
e  Tdead
", header=TRUE)
df1

my.numeric.vec <- scan(text="2 1 4 9 10 3")
names(my.numeric.vec) <- letters[1:6]

# A way
j <- df1
j$my.numeric.vec <- sapply(df1$f, function(y)
my.numeric.vec[ names(my.numeric.vec) %in% y ])
j  # See it

# Another way
merge(df1, data.frame(fileName=names(my.numeric.vec), 
my.numeric.vec=my.numeric.vec))



Hope this helps,

Rui Barradas

Em 10-07-2012 23:38, Adrian Johnson escreveu:

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 cd   ef
2 1 49   10  3



##  my data frame = df1


df1


fileNametype   status
b   N   alive
a   Tdead
d   N   alive
c   T   dead
f   Nalive
e  Tdead


I want to combine as.numeric.vec and df1 and create j with correct
filename and type and numeric value


j


my.numeric.vectype
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@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.



__
R-help@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.


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

2012-07-10 Thread arun
Hi Try this:
df1<-read.table(text="
 fileName    type  status
 b  N  alive
 a  T    dead
 d  N  alive
 c  T  dead
 f  N    alive
 e  T    dead
 ",sep="",header=TRUE)
mynumeric.vec<-c(a=2,b=1,c=4,d=9,e=10,f=3)
mynumeric.df<-data.frame(key=names(mynumeric.vec),Val=mynumeric.vec)
 j<-merge(df1,mynumeric.df,by.x="fileName",by.y="key")
 j<-j[,c(1,4,2)]

colnames(j)<-c("fileName","mynumeric.vec","type")
 j
  fileName mynumeric.vec type
1    a 2    T
2    b 1    N
3    c 4    T
4    d 9    N
5    e    10    T
6    f 3    N


A.K.




- Original Message -
From: Adrian Johnson 
To: r-help 
Cc: 
Sent: Tuesday, July 10, 2012 6:38 PM
Subject: [R] combining numeric vector and column in a data frame

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


__
R-help@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.