Edward
Edward

Reputation: 4623

add columns according to the rule

I have a dataset like this

df = data.frame('a_1' = c(1,0,0), 'b_1' = c(1,0,0), 'c_1' = c(1,0,0), 
           'a_2' = c(0,1,0), 'b_2' = c(0,1,0), 'c_2' = c(0,1,0))

I want to get

> df
  a b c
1 1 1 1
2 1 1 1
3 0 0 0

Of course I can do

df$a = df$a_1+df$a_2
df$b = df$b_1+df$b_2
df$c = df$c_1+df$c_2

and drop redundant columns. But in the real dataset more columns and this way will take a long time. Is there a faster way?I have to add by 'name' not '_number'

Upvotes: 2

Views: 52

Answers (2)

LAP
LAP

Reputation: 6685

In this case, a simple

df1 <- mapply(`+`, df[1:3], df[4:6])

> df1
     a_1 b_1 c_1
[1,]   1   1   1
[2,]   1   1   1
[3,]   0   0   0

would do the trick. Is your real data in a comparable format?


Edit: As @Sotos correctly stated, simply using

df1 <- df[1:3]+df[4:6]

would be enough.

Upvotes: 1

Sotos
Sotos

Reputation: 51592

The idea is to strip the names from their _.* part and add the ones that match, i.e.

sapply(unique(sub('_.*', '', names(df))), function(i) rowSums(df[grepl(i, names(df))]))

which gives,

     a b c
[1,] 1 1 1
[2,] 1 1 1
[3,] 0 0 0

Upvotes: 3

Related Questions