-1

I want a help for a personal project!

i have a df (Name x Date) looking like this

       Name1 Name2 Name3 Name3 Name 4 Name5 Name6 Name 3
Date 1    x1    x2    x3    x4    x5     x6    x7     x8
Date 2    y1    y2    y3    y4    y5     y6    y7     y8
Date 3    z1    z2    z3    z4    z5     z6    z7     y8

etc...

and i want to convert it like this

       Name1 Name2   Name3  Name 4 Name5 Name6
Date 1    x1    x2 x3+x4+x8   x5    x6    x7
Date 2    y1    y2 y3+y4+y8   y5    y6    y7
Date 3    z1    z2 z3+z4+z8   z5    z6    z7

or to create an new df looking like this. Is there anything easy to do? Thank you in advance!

  • 2
    How did you get same column names?? Also please share a reproducible example so we can copy/paste it in our session – Sotos Mar 13 '20 at 13:00
  • 1
    It is very bad practice to have columns with same names. If you had different column names you could use `tidyr` and do `df %>% unite("Name3", c(4,5,9), sep='+')` – camnesia Mar 13 '20 at 13:07
  • I thought that you need exactly this output using this particular dataset, if yes I have solution for this with duplicated column names, so I'll undelete my answer. However as Sotos said, you probably need sum of numerical values of duplicated columns (names), if yes provide reproducible example, however you have the answer already. – Adamm Mar 13 '20 at 13:17

1 Answers1

1

We can use split.default() to split columns with the same name, then take the sum of those columns, i.e.

d1 <- cbind.data.frame(a = sample(5), b = sample(5), a = sample(5), c = sample(5), b = sample(5))
#  a b a c b
#1 1 1 3 5 1
#2 4 3 2 1 5
#3 3 2 5 2 2
#4 5 4 4 3 4
#5 2 5 1 4 3

#Sum columns with the same name
do.call(cbind.data.frame, lapply(split.default(d1, names(d1)), rowSums))
#  a b c
#1 4 2 5
#2 6 8 1
#3 8 4 2
#4 9 8 3
#5 3 8 4
Sotos
  • 51,121
  • 6
  • 32
  • 66