像這樣的東西?
red <- c(1,1,0,0,1,1)
green <- c(0,0,1,1,0,1)
blue <- c(0,0,0,1,1,1)
df <- data.frame(red,green,blue)
df
## red green blue
## 1 1 0 0
## 2 1 0 0
## 3 0 1 0
## 4 0 1 1
## 5 1 0 1
## 6 1 1 1
如果你希望能夠把它們混合起來,就有點亂......
df$redtxt <- ifelse(red==1,"Red","")
df$greentxt <- ifelse(green==1,"Green","")
df$bluetxt <- ifelse(blue==1,"Blue","")
df$all_colors <- with(df, paste0(redtxt,greentxt,bluetxt))
df
## red green blue redtxt greentxt bluetxt all_colors
## 1 1 0 0 Red Red
## 2 1 0 0 Red Red
## 3 0 1 0 Green Green
## 4 0 1 1 Green Blue GreenBlue
## 5 1 0 1 Red Blue RedBlue
## 6 1 1 1 Red Green Blue RedGreenBlue
...但所有你需要的信息是df$all_colors
。
關於第二個問題,有很多方式到那裏,但是這可能是工作...
buncha_dummies <- data.frame(x1=c(0,0,0,1,1),x2=c(0,1,0,1,0),x3=c(0,0,0,0,1))
buncha_dummies$ANY_1 <- with(buncha_dummies, 1*(x1==1 | x2==1 | x3==1))
buncha_dummies
## x1 x2 x3 ANY_1
## 1 0 0 0 0
## 2 0 1 0 1
## 3 0 0 0 0
## 4 1 1 0 1
## 5 1 0 1 1
歡迎SO。請看[如何製作一個很好的重現示例](https://stackoverflow.com/a/5963610/7306168) – Bea