如何在 R 中将两列合并为一列,以便第二列中的每个值成为第一列中的每个其他值?
How to combine two columns into one in R, so that each value in the second column becomes every other value in the first column?
我有以下数据集:
library(plyr)
library(dplyr)
detach(package:plyr)
library(dplyr)
position <- c("A", "A", "B", "C", "D", "E", "F", "F")
level <- c("P", "S", "J", "J", "P", "S", "S", "S")
car <- c("yes", "no", "no", "no", "no", "yes", "no", "yes")
car.data <- data.frame(position, level, car)
percent_car <- car.data %>%
group_by(position) %>%
summarise (car_yes = sum(car == "yes"), car_no = sum(car == "no")) %>%
mutate(yes_perc = car_yes/(car_yes + car_no), no_perc = car_no/(car_yes + car_no))
percent_car
输出为:
position car_yes car_no yes_perc no_perc
<chr> <int> <int> <dbl> <dbl>
1 A 1 1 0.5 0.5
2 B 0 1 0 1
3 C 0 1 0 1
4 D 0 1 0 1
5 E 1 0 1 0
6 F 1 1 0.5 0.5
我的目标是从这个数据集创建圆环图,为此我需要一种形式的数据,其中每个位置都有两行 - 一行用于有车的人,另一行用于人,谁没有。我还想计算每一行的 ymax 和 ymin:
Position yes_no car_yes_no yes_no_perc ymax ymin
<chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 A yes 1 0.5 0.5 0
2 A no 1 0.5 1 0.5
3 B yes 0 0 0 0
4 B no 1 1 1 0
5 C yes 0 0 0 0
6 C no 1 1 1 0
7 D yes 0 0 0 0
8 D no 1 1 1 0
9 E yes 1 1 0 0
10 E no 0 0 1 0
11 F yes 1 0.5 0.5 0
12 F no 1 0.5 1 0.5
目前我正在 Excel 创建第二个数据集。你知道我如何在 R 中直接创建这样的数据集吗?
谢谢!
不完全确定 ymin
/ymax
你的逻辑是什么,但这是一般的想法,运行 逐行查看发生了什么。
percent_car %>%
pivot_longer(names_to = "key", values_to = "value", -position) %>%
mutate(
yes_no = str_extract(key, "yes|no"),
key = str_remove_all(key, "yes|no|_")
) %>%
pivot_wider(names_from = key, values_from = value) %>%
arrange(position) %>%
mutate(
ymax = if_else(yes_no == "yes", perc, 1),
ymin = if_else(yes_no == "yes", 0, 1-perc)
)
)
如果 if_else
需要嵌套 ,case_when
将成为您的朋友
我有以下数据集:
library(plyr)
library(dplyr)
detach(package:plyr)
library(dplyr)
position <- c("A", "A", "B", "C", "D", "E", "F", "F")
level <- c("P", "S", "J", "J", "P", "S", "S", "S")
car <- c("yes", "no", "no", "no", "no", "yes", "no", "yes")
car.data <- data.frame(position, level, car)
percent_car <- car.data %>%
group_by(position) %>%
summarise (car_yes = sum(car == "yes"), car_no = sum(car == "no")) %>%
mutate(yes_perc = car_yes/(car_yes + car_no), no_perc = car_no/(car_yes + car_no))
percent_car
输出为:
position car_yes car_no yes_perc no_perc
<chr> <int> <int> <dbl> <dbl>
1 A 1 1 0.5 0.5
2 B 0 1 0 1
3 C 0 1 0 1
4 D 0 1 0 1
5 E 1 0 1 0
6 F 1 1 0.5 0.5
我的目标是从这个数据集创建圆环图,为此我需要一种形式的数据,其中每个位置都有两行 - 一行用于有车的人,另一行用于人,谁没有。我还想计算每一行的 ymax 和 ymin:
Position yes_no car_yes_no yes_no_perc ymax ymin
<chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 A yes 1 0.5 0.5 0
2 A no 1 0.5 1 0.5
3 B yes 0 0 0 0
4 B no 1 1 1 0
5 C yes 0 0 0 0
6 C no 1 1 1 0
7 D yes 0 0 0 0
8 D no 1 1 1 0
9 E yes 1 1 0 0
10 E no 0 0 1 0
11 F yes 1 0.5 0.5 0
12 F no 1 0.5 1 0.5
目前我正在 Excel 创建第二个数据集。你知道我如何在 R 中直接创建这样的数据集吗?
谢谢!
不完全确定 ymin
/ymax
你的逻辑是什么,但这是一般的想法,运行 逐行查看发生了什么。
percent_car %>%
pivot_longer(names_to = "key", values_to = "value", -position) %>%
mutate(
yes_no = str_extract(key, "yes|no"),
key = str_remove_all(key, "yes|no|_")
) %>%
pivot_wider(names_from = key, values_from = value) %>%
arrange(position) %>%
mutate(
ymax = if_else(yes_no == "yes", perc, 1),
ymin = if_else(yes_no == "yes", 0, 1-perc)
)
)
如果 if_else
需要嵌套 ,case_when
将成为您的朋友