在 R 中,根据行中的值将行转换为列
Converting rows into columns based on the values in the rows, in R
这是我正在使用的table。我想将 reservation
转换成单独的列。
我想让它变成这样的。我一直在尝试使用 reshape2
和 dplyr
的 separate
来做到这一点,但我没有找到解决方案。
你可以试试
library(tidyverse)
df %>%
rowwise %>%
mutate(reservation_main = str_split(reservation,'_' ,simplify = T)[1],
reservation_no = paste0('_',str_split(reservation,'_' ,simplify = T)[2])) %>%
select(id, response_id, reservation_main, reservation_no) %>%
pivot_wider(names_from = reservation_no, values_from = response_id)
id reservation_main `_1` `_2` `_3`
<dbl> <chr> <dbl> <dbl> <dbl>
1 31100 A 1 1 0
2 31100 B 1 1 0
3 31100 C 1 0 0
由于reservation
格式一致,我们可以用_
分隔成两列。然后,我们可以将 response
转换为 0 和 1。然后,我删除 response_id
。最后,我转向宽幅面。我假设您不希望在列中的数字之前使用 _
。
library(tidyverse)
df %>%
separate(reservation, c("reservation", "number"), sep = "_") %>%
mutate(response = ifelse(response == "yes", 1, 0)) %>%
select(-response_id) %>%
pivot_wider(names_from = "number", values_from = "response")
输出
# A tibble: 2 × 5
id reservation `1` `2` `3`
<dbl> <chr> <dbl> <dbl> <dbl>
1 31100 A 1 1 0
2 31100 B 1 1 0
如果您确实希望将 _
保留在列的数字前面,那么我们可以调整 separate
.
中的正则表达式
df %>%
separate(reservation, c("reservation", "number"), sep = "(?=\_)") %>%
mutate(response = ifelse(response == "yes", 1, 0)) %>%
select(-response_id) %>%
pivot_wider(names_from = "number", values_from = "response")
# A tibble: 2 × 5
id reservation `_1` `_2` `_3`
<dbl> <chr> <dbl> <dbl> <dbl>
1 31100 A 1 1 0
2 31100 B 1 1 0
数据
df <- structure(
list(
id = c(31100, 31100, 31100, 31100, 31100, 31100),
reservation = c("A_1", "A_2", "A_3", "B_1", "B_2", "B_3"),
response = c("yes", "yes", "no", "yes", "yes", "no"),
response_id = c(1,
1, 0, 1, 1, 0)
),
class = "data.frame",
row.names = c(NA,-6L)
)
这是我正在使用的table。我想将 reservation
转换成单独的列。
我想让它变成这样的。我一直在尝试使用 reshape2
和 dplyr
的 separate
来做到这一点,但我没有找到解决方案。
你可以试试
library(tidyverse)
df %>%
rowwise %>%
mutate(reservation_main = str_split(reservation,'_' ,simplify = T)[1],
reservation_no = paste0('_',str_split(reservation,'_' ,simplify = T)[2])) %>%
select(id, response_id, reservation_main, reservation_no) %>%
pivot_wider(names_from = reservation_no, values_from = response_id)
id reservation_main `_1` `_2` `_3`
<dbl> <chr> <dbl> <dbl> <dbl>
1 31100 A 1 1 0
2 31100 B 1 1 0
3 31100 C 1 0 0
由于reservation
格式一致,我们可以用_
分隔成两列。然后,我们可以将 response
转换为 0 和 1。然后,我删除 response_id
。最后,我转向宽幅面。我假设您不希望在列中的数字之前使用 _
。
library(tidyverse)
df %>%
separate(reservation, c("reservation", "number"), sep = "_") %>%
mutate(response = ifelse(response == "yes", 1, 0)) %>%
select(-response_id) %>%
pivot_wider(names_from = "number", values_from = "response")
输出
# A tibble: 2 × 5
id reservation `1` `2` `3`
<dbl> <chr> <dbl> <dbl> <dbl>
1 31100 A 1 1 0
2 31100 B 1 1 0
如果您确实希望将 _
保留在列的数字前面,那么我们可以调整 separate
.
df %>%
separate(reservation, c("reservation", "number"), sep = "(?=\_)") %>%
mutate(response = ifelse(response == "yes", 1, 0)) %>%
select(-response_id) %>%
pivot_wider(names_from = "number", values_from = "response")
# A tibble: 2 × 5
id reservation `_1` `_2` `_3`
<dbl> <chr> <dbl> <dbl> <dbl>
1 31100 A 1 1 0
2 31100 B 1 1 0
数据
df <- structure(
list(
id = c(31100, 31100, 31100, 31100, 31100, 31100),
reservation = c("A_1", "A_2", "A_3", "B_1", "B_2", "B_3"),
response = c("yes", "yes", "no", "yes", "yes", "no"),
response_id = c(1,
1, 0, 1, 1, 0)
),
class = "data.frame",
row.names = c(NA,-6L)
)