I have a set of five columns in a data table.
dt <- data.table(
city = c(rep(1,2), rep(2,2), rep(3,2), rep(4,2)),
neighborhoods.1 = c(NA, "a", "b", "c", NA, NA, "d", "e"),
neighborhoods.2 = c(NA, "f", "g", rep(NA,5)),
neighborhoods.3 = c(NA, "h", rep(NA, 6)),
irrelevantdata = c(1:8)
)
city neighborhoods.1 neighborhoods.2 neighborhoods.3 irrelevantdata
1: 1 NA NA NA 1
2: 1 a f h 2
3: 2 b g NA 3
4: 2 c NA NA 4
5: 3 NA NA NA 5
6: 3 NA NA NA 6
7: 4 d NA NA 7
8: 4 e NA NA 8
I want to combine the first four columns into one column.
neighborhood
1: 1
2: 1-a-f-h
3: 2-b-g
4: 2-c
5: 3
6: 3
7: 4-d
8: 4-e
As you can see, I delete the entries NAand sharing them with -.
I tried this, which has obvious processing problems j:
business[
,
neighborhood = paste0(
city,
if(!is.na(neighborhoods.1)) paste0("-", neighborhoods.1),
if(!is.na(neighborhoods.2)) paste0("-", neighborhoods.2),
if(!is.na(neighborhoods.3)) paste0("-", neighborhoods.3),
""
)
]
How can i do this?
Updated to reflect that there are additional columns that I do not want to combine.