I have this dataset
data<-data.frame(column1=c("A","B","C"),column2=c(0.01,0.01,0.01))
and I want to merge the last column, it means display an the table with same value in the last columns, like showed below:
is it possible ?
CodePudding user response:
Replace duplicated items with space. Note that column2 is no longer numeric. No packages are used.
transform(data, column2 = ifelse(duplicated(column2), "", column2))
giving:
column1 column2
1 A 0.01
2 B
3 C
This also works and gives the same result for this data frame but in an expanded data frame it would only replace duplicates that are consecutive so which one to use would depend on what you want.
library(data.table)
spacepad <- function(x) replace(x, -1, "")
transform(data, column2 = ave(column2, rleid(column2), FUN = spacepad))