Java Mailing List Archive

http://www.r-help.com/

Home » Home (12/2007) » R Help for Statistical Computing »

Re: [R] filling columns in frame according to another column
 frame

Luis Rideau Cruz

2005-02-23


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@(protected)
>>>
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@(protected)
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
>

______________________________________________
R-help@(protected)
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
©2008 r-help.com - Jax Systems, LLC, U.S.A.