How to convert three columns into single one in R -
i have dataset looks this:
v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 -1 3 1 1 1 -1 -1 -1 -1 1 1 1 1 -1 -1 -1 -1 4 1 1 1 -1 -1 -1 -1 -1 -1 -1 -1 1 1 1 1 5 1 -1 -1 1 1 -1 -1 1 1 -1 -1 1 1 -1 -1 6 1 -1 -1 1 1 -1 -1 -1 -1 1 1 -1 -1 1 1 7 1 -1 -1 -1 -1 1 1 1 1 -1 -1 -1 -1 1 1 8 1 -1 -1 -1 -1 1 1 -1 -1 1 1 1 1 -1 -1 9 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 10 -1 1 -1 1 -1 1 -1 -1 1 -1 1 -1 1 -1 1 11 -1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 12 -1 1 -1 -1 1 -1 1 -1 1 -1 1 1 -1 1 -1 13 -1 -1 1 1 -1 -1 1 1 -1 -1 1 1 -1 -1 1 14 -1 -1 1 1 -1 -1 1 -1 1 1 -1 -1 1 1 -1 15 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1 16 -1 -1 1 -1 1 1 -1 -1 1 1 -1 1 -1 -1 1 and want combine first 3 colums single base on principle:
111 → 1 1 -1 -1→ 2 -1 1 -1→ 3 -1 -1 1→ 4 this first time use r language. have no idea how it. have simple pieces of code this? in advanced!
i pretend data frame called df...
test <- apply(df[1:3], 1, paste, collapse="") # merge numbers of first 3 # each row result <- sapply(test, switch, '111' = 1, '1-11' = 2, '-11-1' = 3, '-1-11' = 4) in case result list use unlist
Comments
Post a Comment