R 创建具有分组数据级别的变量
R Create a variable with the levels of grouped data
我有一个数据框,例如data
data = data.frame(ID = as.factor(c("A", "A", "B","B","C","C")),
var.color= as.factor(c("red", "blue", "green", "red", "green", "yellow")))
我想知道是否可以获取ID
中每个组的级别(例如A
、B
、C
)并创建一个粘贴的变量他们。我已尝试通过 运行 以下方式这样做:
data %>% group_by(ID) %>%
mutate(ex = paste(droplevels(var.color), sep = "_"))
产生:
Source: local data frame [6 x 3]
Groups: ID [3]
ID var.color ex
<fctr> <fctr> <chr>
1 A red red
2 A blue blue
3 B green red
4 B red red
5 C green green
6 C yellow yellow
但是,我想要的 data.frame
应该是这样的:
ID var.color ex
<fctr> <fctr> <chr>
1 A red red_blue
2 A blue red_blue
3 B green green_red
4 B red green_red
5 C green green_yellow
6 C yellow green_yellow
基本上,您需要 collapse
而不是 sep
您可以将按 ID
分组的文本粘贴在一起,而不是降低级别
library(dplyr)
data %>% group_by(ID) %>%
mutate(ex = paste(var.color, collapse = "_"))
# ID var.color ex
# <fctr> <fctr> <chr>
#1 A red red_blue
#2 A blue red_blue
#3 B green green_red
#4 B red green_red
#5 C green green_yellow
#6 C yellow green_yellow
你可以使用循环来做同样的事情
for(i in unique(data$ID)){
data$ex[data$ID==i] <- paste0(data$var.color[data$ID==i], collapse = "_")
}
> data
ID var.color ex
1 A red red_blue
2 A blue red_blue
3 B green green_red
4 B red green_red
5 C green green_yellow
6 C yellow green_yellow
我有一个数据框,例如data
data = data.frame(ID = as.factor(c("A", "A", "B","B","C","C")),
var.color= as.factor(c("red", "blue", "green", "red", "green", "yellow")))
我想知道是否可以获取ID
中每个组的级别(例如A
、B
、C
)并创建一个粘贴的变量他们。我已尝试通过 运行 以下方式这样做:
data %>% group_by(ID) %>%
mutate(ex = paste(droplevels(var.color), sep = "_"))
产生:
Source: local data frame [6 x 3]
Groups: ID [3]
ID var.color ex
<fctr> <fctr> <chr>
1 A red red
2 A blue blue
3 B green red
4 B red red
5 C green green
6 C yellow yellow
但是,我想要的 data.frame
应该是这样的:
ID var.color ex
<fctr> <fctr> <chr>
1 A red red_blue
2 A blue red_blue
3 B green green_red
4 B red green_red
5 C green green_yellow
6 C yellow green_yellow
基本上,您需要 collapse
而不是 sep
您可以将按 ID
library(dplyr)
data %>% group_by(ID) %>%
mutate(ex = paste(var.color, collapse = "_"))
# ID var.color ex
# <fctr> <fctr> <chr>
#1 A red red_blue
#2 A blue red_blue
#3 B green green_red
#4 B red green_red
#5 C green green_yellow
#6 C yellow green_yellow
你可以使用循环来做同样的事情
for(i in unique(data$ID)){
data$ex[data$ID==i] <- paste0(data$var.color[data$ID==i], collapse = "_")
}
> data
ID var.color ex
1 A red red_blue
2 A blue red_blue
3 B green green_red
4 B red green_red
5 C green green_yellow
6 C yellow green_yellow