Dear All, I have two data like this : $cat main.csv name,id,memory,storage mohan,1,100.20,1.10 ram,1,200,100 kumar,1,400,50 xxx,1,100,40 aaa,1,800,45 mount,1,200,80> main <- read.csv(file='main.csv',sep=',' , header=TRUE) > mainname id memory storage 1 mohan 1 100.2 10 2 ram 1 200.0 100 3 kumar 1 400.0 50 4 xxx 1 100.0 40 5 aaa 1 800.0 45 6 mount 1 200.0 80 $cat other.csv name,ip,bsent,breceived mohan,1,12.00,0.01 xxx,1,00.00,1.110 kumat,1,1.00,1.00 mmm,1,10.00,8.08 own,1,20.13,12.08 per,1,1.89,0.89> other <- read.csv(file='other.csv',sep=',' , header=TRUE) > othername ip bsent breceived 1 mohan 1 12.00 0.01 2 xxx 1 0.00 1.11 3 kumat 1 1.00 1.00 4 mmm 1 10.00 8.08 5 own 1 20.13 12.08 6 per 1 1.89 0.89 I want to merge ip,bsent,breceived column to "main" , If the name in the the "main" data frame is there in the "other" data frame . some thinng like this: name id memory storage ip bsent breceived mohan 1 100 20 1 12.00 0.01 ram 1 200 100 0 00.00 0.00 kumar 1 400 50 1 1.00 1.00 xxx 1 100 40 1 00.00 1.110 aaa 1 800 45 0 00.00 00.00 mount 1 200 80 0 00.00 00.00 If in case the name in the "main" data frame does not there in the "other" data frame, simple I want to add zero to ip,bsent,breceived value. I hope this can be done with R. Any help will appricated. Thanks for you time. Thanks & Rg Mohan L
On 09.11.2010 16:17, Mohan L wrote:> Dear All, > > I have two data like this : > > $cat main.csv > > name,id,memory,storage > mohan,1,100.20,1.10 > ram,1,200,100 > kumar,1,400,50 > xxx,1,100,40 > aaa,1,800,45 > mount,1,200,80 > >> main<- read.csv(file='main.csv',sep=',' , header=TRUE) >> main > name id memory storage > 1 mohan 1 100.2 10 > 2 ram 1 200.0 100 > 3 kumar 1 400.0 50 > 4 xxx 1 100.0 40 > 5 aaa 1 800.0 45 > 6 mount 1 200.0 80 > > > $cat other.csv > name,ip,bsent,breceived > mohan,1,12.00,0.01 > xxx,1,00.00,1.110 > kumat,1,1.00,1.00 > mmm,1,10.00,8.08 > own,1,20.13,12.08 > per,1,1.89,0.89 > > >> other<- read.csv(file='other.csv',sep=',' , header=TRUE) >> other > name ip bsent breceived > 1 mohan 1 12.00 0.01 > 2 xxx 1 0.00 1.11 > 3 kumat 1 1.00 1.00 > 4 mmm 1 10.00 8.08 > 5 own 1 20.13 12.08 > 6 per 1 1.89 0.89 > > > > I want to merge ip,bsent,breceived column to "main" , If the name in > the the "main" data frame is there in the "other" data frame . some > thinng like this: > > name id memory storage ip bsent breceived > > mohan 1 100 20 1 12.00 0.01 > ram 1 200 100 0 00.00 0.00 > kumar 1 400 50 1 1.00 1.00 > xxx 1 100 40 1 00.00 1.110 > aaa 1 800 45 0 00.00 00.00 > mount 1 200 80 0 00.00 00.00 > > > If in case the name in the "main" data frame does not there in the > "other" data frame, simple I want to add zero to ip,bsent,breceived > value. > > > I hope this can be done with R. Any help will appricated.See ?merge including its argument all=TRUE. Uwe Ligges> Thanks for you time. > > Thanks& Rg > Mohan L > > ______________________________________________ > 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.
Marianne Promberger
2010-Nov-09 15:41 UTC
[R] help to merge two data frame if name matches
Hi Mohan, Mohan L <l.mohanphysics at gmail.com> 09-Nov-10 15:17:> I want to merge ip,bsent,breceived column to "main" , If the name in > the the "main" data frame is there in the "other" data frame . some > thinng like this: > > name id memory storage ip bsent breceived > > mohan 1 100 20 1 12.00 0.01 > ram 1 200 100 0 00.00 0.00 > kumar 1 400 50 1 1.00 1.00 > xxx 1 100 40 1 00.00 1.110 > aaa 1 800 45 0 00.00 00.00 > mount 1 200 80 0 00.00 00.00 > > > If in case the name in the "main" data frame does not there in the > "other" data frame, simple I want to add zero to ip,bsent,breceived > value.Is this what you want? newdat <- merge(main,other,by="name",all.x=T) name id memory storage ip bsent breceived 1 aaa 1 800.0 45.0 NA NA NA 2 kumar 1 400.0 50.0 NA NA NA 3 mohan 1 100.2 1.1 1 12 0.01 4 mount 1 200.0 80.0 NA NA NA 5 ram 1 200.0 100.0 NA NA NA 6 xxx 1 100.0 40.0 1 0 1.11 newdat[is.na(newdat)] <- 0 newdat name id memory storage ip bsent breceived 1 aaa 1 800.0 45.0 0 0 0.00 2 kumar 1 400.0 50.0 0 0 0.00 3 mohan 1 100.2 1.1 1 12 0.01 4 mount 1 200.0 80.0 0 0 0.00 5 ram 1 200.0 100.0 0 0 0.00 6 xxx 1 100.0 40.0 1 0 1.11 Marianne -- Marianne Promberger PhD, King's College London http://promberger.info R version 2.12.0 (2010-10-15) Ubuntu 9.04