我有列“家庭”、“每个家庭中的人数”、“旅行”(每个旅行包含每个人的不同旅行)、“旅行”(每个旅行中的旅行次数)和“模式”(每个人在每次旅行中的旅行方式)
我想要更改模式列相对于旅游列如下
心情==汽车,如果旅游中至少存在一次使用模式汽车的行程
心情==非汽车,如果旅行中没有行程有模式=汽车
示例:
household. person. trip. tour. mode
1 1 1 1 car
1 1 2 1 walk
1 1 4 1 bus
1 1 1 2 bus
1 1 2 2 walk
1 2 1 1 walk
1 2 2 1 bus
1 2 3 1 walk
2 1 1 1 walk
2 1 1 1 car
输出
household. person. trip. tour. mode
1 1 1 1 car
1 1 2 1 car
1 1 4 1 car
1 1 1 2 non-car
1 1 2 2 non-car
1 2 1 1 non-car
1 2 2 1 non-car
1 2 3 1 non-car
2 1 1 1 car
2 1 1 1 car
最佳答案
我们可以按“家庭”、“人”、“旅游”进行分组。并通过检查列中是否有“car”将“mode”更改为两个值。在这种情况下,通过添加 1(TRUE -> 2,FALSE ->1)将其转换为数字索引,并基于此索引,我们传递一个字符串向量来替换索引
library(dplyr)
df1 %>%
group_by(household., person., tour.) %>%
mutate(mode = c('non-car', 'car')[1+any(mode == "car")])
# A tibble: 10 x 5
# Groups: household., person., tour. [4]
# household. person. trip. tour. mode
# <int> <int> <int> <int> <chr>
# 1 1 1 1 1 car
# 2 1 1 2 1 car
# 3 1 1 4 1 car
# 4 1 1 1 2 non-car
# 5 1 1 2 2 non-car
# 6 1 2 1 1 non-car
# 7 1 2 2 1 non-car
# 8 1 2 3 1 non-car
# 9 2 1 1 1 car
#10 2 1 1 1 car
数据
df1 <- structure(list(household. = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L), person. = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L),
trip. = c(1L, 2L, 4L, 1L, 2L, 1L, 2L, 3L, 1L, 1L), tour. = c(1L,
1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L), mode = c("car", "walk",
"bus", "bus", "walk", "walk", "bus", "walk", "walk", "car"
)), class = "data.frame", row.names = c(NA, -10L))
关于r - 如何更改数据集中某些信息的列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57258021/