在 R 中,根据某些条件将一列拆分为两列

In R, split a column in 2 columns based on some condition

我正在尝试根据百分比将数据框租用分成两列。

|集团|百分比|

|0 人入职 | 60% |

| 0 人入职 next_month | 65% |

| 0 或 1 人聘用 | 68% |

|雇用 0 或 1 人 next_month | 70%|

|1 人入职 | 79%|

|1 或 2 名员工 | 80%|

|2 人退休| 85%|

|2 或 3 次发射| 92%|

|3未退休| 96%|

|集团|决定 |百分比


| 0 |聘用 | 60%

| 0 |雇用 next_month | 65%

| 0 |聘用 | 68%

| 1 |雇用 next_month | 70%

| 1 |聘用 | 79%

|2 |雇员 | 80%|

|2 |退休了| 85%|

|3 |解雇| 92%|

|3 |未退休| 96%|

我的代码: foo<-str_split_fixed(hire$group, "or", 2)

谁能帮忙。提前致谢

我们可以用tidyverseextract(来自tidyr)将'group'拆分为'group'和'decision',然后replace 如果 'percentage'(从 readr 中提取带有 parse_number 的数字)大于或等于 90,则 'group' 值为 1。

library(tidyverse)
df1 %>%
    extract(group, into = c('group', 'decision'), "^(\d+).*(hired.*)") %>% 
    mutate(group = replace(group, parse_number(percentage)>=90, 1))
#    group         decision percentage
#1     0            hired        80%
#2     0 hired next_month        85%
#3     0            hired        88%
#4     1 hired next_month        90%
#5     1            hired        99%

数据

df1 <- structure(list(group = c("0 hired", "0 hired next_month", "0 or 1 hired", 
"0 or 1 hired next_month", "1 hired"), percentage = c("80%", 
"85%", "88%", "90%", "99%")), .Names = c("group", "percentage"
), class = "data.frame", row.names = c(NA, -5L))