[R] filling columns in frame according to another column frame
Adaikalavan Ramasamy
ramasamy at cancer.org.uk
Wed Feb 23 17:10:38 CET 2005
So you want to grep for each pattern as indicated by columns of test2 in
the columns of test1. Something like your initial approach.
p <- c(1,3,5) # pattern like test2$'cm'
x <- sample(1:10) # data like test1$'cm'
x
[1] 3 4 9 8 7 5 10 2 1 6
x %in% p
[1] TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE
(w <- which( x %in% p ))
[1] 1 6 9
If no matches are found, you get numeric(0) for 'w' and I usually test
this using length(w)==0.
On second thought, I tried your original solution and it appears to work
but not tested thoroughly. Can you provide a _simple_ example where this
does not work. Thank you.
z <- 1:length(x) # like test1$'3'
z
[1] 1 2 3 4 5 6 7 8 9 10
ifelse( x %in% p, z, 0 )
[1] 1 0 0 0 0 6 0 0 9 0
BTW, why are you quoting your names as in 'cm' and '3' and are test1,
test2 really data.frames ? What does class(test1) and class(test1$'cm')
say ?
Regards, Adai
On Wed, 2005-02-23 at 15:38 +0000, Luis Ridao Cruz wrote:
> I think I did not explain very well what my problem is,
>
> Both frames have the same number of columns but different number of
> rows.
> The point is to compare 'cm' column in test1 with 'cm' in test2 as
> follows
>
> first element in test2$'cm' versus first element in test1$'cm'
> first element in test2$'cm' versus second element in test1$'cm'
> first element in test2$'cm' versus element element in test1$'cm'
> .....
>
>
> if any of the above matches returns the value in column , lets say 10,
> of element in test1$'cm' , if not then 0
>
> Luis
>
>
> >>> Adaikalavan Ramasamy <ramasamy at cancer.org.uk> 23/02/2005 15:07:55
> >>>
> I am confused. Are you saying that your two data frames are of
> different
> dimensions ?
>
> In any case what I think what you are looking for is which.
>
> # generate the conditioning matrix
> a <- matrix( sample(0:1, 9, replace=TRUE), nc=3 )
> a
> [,1] [,2] [,3]
> [1,] 1 1 1
> [2,] 1 0 0
> [3,] 0 0 1
>
> # find the index where zero is present
> ( w <- which( a == 0, arr.ind=T ) )
> row col
> [1,] 3 1
> [2,] 2 2
> [3,] 3 2
> [4,] 2 3
>
> # generate the matrix of interest
> ( b <- matrix(1:9, nc=3, byrow=T) )
> [,1] [,2] [,3]
> [1,] 1 2 3
> [2,] 4 5 6
> [3,] 7 8 9
>
> # values that will be used to impute the zero's in a
> b[w]
> [1] 7 5 8 6
>
> # impute the values of a with b where a is zero
> a[w] <- b[w]
>
> # the result
> a
> [,1] [,2] [,3]
> [1,] 1 1 1
> [2,] 1 5 6
> [3,] 7 8 1
>
>
> Regards, Adai
>
>
> On Wed, 2005-02-23 at 14:14 +0000, Luis Ridao Cruz wrote:
> > R-help,
> >
> > I have a frame which I want to fill up conditioning to another data
> > frame column.
> >
> > The one I want to fill up is as follows (basically an empty one):
> >
> > > test2
> >
> > cm 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
> > 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
> 1
> > 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
> 2
> > 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
> 3
> > 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
> 4
> > 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
> 5
> > 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
> 6
> > 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7
> 7
> > 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
> 8
> > 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
> 9
> >
> > The other looks like :
> >
> > > test1
> >
> > cm 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
> > 38 0 0 1 0 0 0 6 0 0 0 0 0 0 0 0 0
> > 39 0 0 1 0 0 0 0 0 0 0 0 0 6 0 0 0
> > 40 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
> > 41 0 0 2 0 0 0 0 0 0 0 6 0 0 0 0 0
> > 43 0 0 1 0 0 0 4 0 0 0 0 0 0 0 0 0
> > 44 0 0 4 0 0 0 5 0 0 0 0 0 0 0 0 0
> > 45 0 0 2 0 0 0 0 0 0 0 6 0 0 0 0 0
> > 47 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0
> > 48 0 0 2 0 0 0 0 0 0 6 0 0 0 0 0 0
> > 49 0 0 2 0 0 0 0 0 0 6 0 0 0 0 0 0
> > 50 0 0 3 0 0 0 0 0 0 3 0 0 0 0 0 0
> > 51 0 0 2 0 0 0 0 0 0 3 0 0 0 0 0 0
> >
> > Length of both frames are different ( test2 = 150 and test1 = 70 )
> > The key column is 'cm'
> >
> > I have tried someting (fill column '3' in test2):
> >
> > test2 [, '3' ]<-
> > ifelse ( test2$'cm' %in% test1$'cm' , test1$'3' , 0)
> >
> > but the result is wrong.
> >
> > Any suggestions?
> >
> > Thanks in advance
> >
> >
> > > version
> > _
> > platform i386-pc-mingw32
> > arch i386
> > os mingw32
> > system i386, mingw32
> > status
> > major 2
> > minor 0.1
> > year 2004
> > month 11
> > day 15
> > language R
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
> >
>
>
More information about the R-help
mailing list