Because i am dealing with a survey, there is no difference when df$vec3==3 and I want to assign when vec3==3, 1 and 2
vec1 <- c("1","2","3","4","5","6")
vec2 <- c("a","s","d","f","g","h")
vec3 <- c("1","2","3","3","3","3")
df <- cbind(vec1,vec2,vec3)
df
vec1 vec2 vec3
[1,] "1" "a" "1"
[2,] "2" "s" "2"
[3,] "3" "d" "3"
[4,] "4" "f" "3"
[5,] "5" "g" "3"
[6,] "6" "h" "3"
Expected answer
vec1 vec2 vec3 vec4
[1,] "1" "a" "1" "1"
[2,] "2" "s" "2" "2"
[3,] "3" "d" "3" "1"
[4,] "4" "f" "3" "2"
[5,] "5" "g" "3" "1"
[6,] "6" "h" "3" "2"
We can use
ifelseand repeat1:2for number of times we havevec3 == 3.Or using
dplyr:Or a simpler approach using vector recycling.
data