[R] Combining two tables without going through lot of ifelse statement
Rui Barradas
ruipbarradas at sapo.pt
Fri Aug 23 15:31:38 CEST 2013
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-08-2013 13:59, Anindya Sankar Dey escreveu:
> 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.
>
More information about the R-help
mailing list