Remove duplicates from column ID but keep values in new columns in R

1

I have this data.frame :

c_dupli <- data.frame(id=c(1,1,2), value=c(10,20,30))

My intention is to leave the id unique, but keep the value different in another / s column / s. The expected result is the following:

s_dupli <- data.frame(id=c(1,2), value1=c(10,30), value2=c(20,"NA"))

Any suggestions? Thanks.

    
asked by Aday Hernández 03.06.2017 в 02:13
source

1 answer

0

For now, the form that occurs to me is the following:

library(data.table)
dcast(c_dupli, id ~ rowid(id, prefix = "value"), value.var = "value")

Using reshape we can also do this:

# agregamos un numerador por grupo
c_dupli$num <- ave(c_dupli$id, c_dupli$id, FUN = seq_along)
reshape(c_dupli, idvar="id", timevar="num", direction="wide")

The output in all cases is similar to this:

  id value1 value2
1  1     10     20
2  2     30     NA

Greetings ..

    
answered by 03.06.2017 в 17:13