The following problem:
I have the data frame data1 with a variable including several entries:
data1 <- data.frame(v1 = c("test, test, bird", "bird, bird", "car"))
Now I want to remove duplicated entries in each row. The result should look like this:
data1.final <- data.frame(v1 = c("test, bird", "bird", "car"))
I tried this:
data1$ID <- 1:nrow(data1)
data1$v1 <- as.character(data1$v1)
data1 <- split(data1, data1$ID)
reduce.words <- function(x) {
d <- unlist(strsplit(x$v1, split=" "))
d <- paste(d[-which(duplicated(d))], collapse = ' ')
x$v1 <- d
return(x)
}
data1 <- lapply(data1, reduce.words)
data1 <- as.data.frame(do.call(rbind, data1))
However, this yields empty rows, except the first one. Anyone an idea to solve this problem?