a Sankar Dey
To: arun
Cc: R help
Sent: Friday, August 23, 2013 9:55 AM
Subject: Re: [R] Combining two tables without going through lot of ifelse
statement
Hi All,
Arun's solution is working.
Now can someone help me in just an expansion.
If we have multiple table like this, adding them
2 10
> #4 3 10
> A.K.
> ____________
> From: Anindya Sankar Dey
> To: arun
> Sent: Friday, August 23, 2013 9:40 AM
> Subject: Re: [R] Combining two tables without going through lot of ifelse
> statement
>
>
>
> Mine is matrices, will this
different in each case. Here, I
assumed that your table is data.frame..
>
>
>
>
>- Original Message -
>From: arun
>To: Anindya Sankar Dey
>Cc: R help
>Sent: Friday, August 23, 2013 9:30 AM
>Subject: Re: [R] Combining two tables without going through
Hello,
Try the following.
x1 <- read.table(text = "
1 10
3 5
0 0
")
x2 <- read.table(text = "
2 10
0 0
3 5
")
x3 <- merge(x1, x2, by = "V1", all = TRUE)
res <- data.frame(x3[1], sum = rowSums(x3[-1], na.rm = TRUE))
res <- res[-(res$sum == 0), ]
res
Hope this helps,
Rui Barradas
Em 23-
0
#4 3 10
#or
library(data.table)
dt1<- data.table(rbind(dat1,dat2))
dt2<-subset(dt1[,sum(V2),by=V1],V1!=0)
setnames(dt2,2,"V2")
dt2
# V1 V2
#1: 1 10
#2: 3 10
#3: 2 10
A.K.
- Original Message -
From: Anindya Sankar Dey
To: r-help
Cc:
Sent: Friday, August 23, 2013
HI All,
Suppose I have two table like below
Table 1:
1 10
3 5
0 0
Table 2:
2 10
0 0
3 5
I need to create a new table like below
Table 3:
1 10
2 10
3 10
The row may interchange in table 3, but is there any way to do this instead
of writing lot of if-else and loops?
Thanks in advance.
6 matches
Mail list logo