仅当其存在时将字符串的第三部分提取到新列
我的数据如下:
数据
library(dplyr)
dat_in_one <- structure(list(rn = c("Type_A", "Type_B"
), `[0,25)` = c(5L, 0L), `[25,50)` = c(0L, 0L), `[25,100)` = c(38L,
3L), `[50,100)` = c(0L, 0L), `[100,250)` = c(43L, 5L), `[100,500)` = c(0L,
0L), `[250,500)` = c(27L, 12L), `[500,1000)` = c(44L, 0L), `[1000,1500)` = c(0L,
0L), `[1500,3000)` = c(0L, 0L), `[500,1000000]` = c(0L, 53L),
`[1000,1000000]` = c(20L, 0L), `[3000,1000000]` = c(0L, 0L
), Sum_bin = c(177, 73), strata = list(c(0, 25, 100, 250,
500, 1000, 1e+06), c(0, 25, 100, 250, 500, 1e+06))), row.names = c(NA,
-2L), class = c("data.table", "data.frame"))
library(dplyr)
dat_in_two <- structure(list(rn = c("Type_A", "Type_B"
), `[0,25) East` = c(5L, 0L), `[25,50) South` = c(0L, 0L), `[25,100) West` = c(38L,
3L), `[50,100) North` = c(0L, 0L), `[100,250) East` = c(43L, 5L), `[100,500) South` = c(0L,
0L), `[250,500) South` = c(27L, 12L), `[500,1000) South` = c(44L, 0L), `[1000,1500) South` = c(0L,
0L), `[1500,3000) South` = c(0L, 0L), `[500,1000000] East` = c(0L, 53L),
`[1000,1000000] South` = c(20L, 0L), `[3000,1000000] South` = c(0L, 0L
), Sum_bin = c(177, 73), strata = list(c(0, 25, 100, 250,
500, 1000, 1e+06), c(0, 25, 100, 250, 500, 1e+06))), row.names = c(NA,
-2L), class = c("data.table", "data.frame"))
问题
我想在下面调整此代码(从strata
列提取数字并从中创建两个新列):
dat_in_one %>%
pivot_longer(-c(rn, strata)) %>%
extract(name, c('lower', 'upper'), '(\\d+),(\\d+)', convert = TRUE)
# A tibble: 28 x 5
rn strata lower upper value
<chr> <list> <int> <int> <dbl>
1 Type_A <dbl [7]> 0 25 5
2 Type_A <dbl [7]> 25 50 0
3 Type_A <dbl [7]> 25 100 38
4 Type_A <dbl [7]> 50 100 0
5 Type_A <dbl [7]> 100 250 43
6 Type_A <dbl [7]> 100 500 0
# ... with 22 more rows
它当前忽略了east
,south
et ef dat_in_two
中的术语。我想尝试调整此代码,同时处理dat_in_one
和dat_in_two
,其中dat_in_two
它创建了第三列。我试图做,但根本不起作用。
dat_in_two %>%
pivot_longer(-c(rn, strata)) %>%
extract(name, c('lower', 'upper', 'the_rest'), '(\\d+),(\\d+),(\\w+)', convert = TRUE)
dat_in_one的期望结果
# A tibble: 28 x 5
rn strata lower upper value
<chr> <list> <int> <int> <dbl>
1 Type_A <dbl [7]> 0 25 5
2 Type_A <dbl [7]> 25 50 0
3 Type_A <dbl [7]> 25 100 38
4 Type_A <dbl [7]> 50 100 0
5 Type_A <dbl [7]> 100 250 43
6 Type_A <dbl [7]> 100 500 0
# ... with 22 more rows
dat_in_two的期望结果
# A tibble: 28 x 5
rn strata lower upper rest value
<chr> <list> <int> <int> <char> <dbl>
1 Type_A <dbl [7]> 0 25 East 5
2 Type_A <dbl [7]> 25 50 South 0
3 Type_A <dbl [7]> 25 100 West 38
4 Type_A <dbl [7]> 50 100 North 0
5 Type_A <dbl [7]> 100 250 East 43
6 Type_A <dbl [7]> 100 500 South 0
# ... with 22 more rows
I have data as follows:
Data
library(dplyr)
dat_in_one <- structure(list(rn = c("Type_A", "Type_B"
), `[0,25)` = c(5L, 0L), `[25,50)` = c(0L, 0L), `[25,100)` = c(38L,
3L), `[50,100)` = c(0L, 0L), `[100,250)` = c(43L, 5L), `[100,500)` = c(0L,
0L), `[250,500)` = c(27L, 12L), `[500,1000)` = c(44L, 0L), `[1000,1500)` = c(0L,
0L), `[1500,3000)` = c(0L, 0L), `[500,1000000]` = c(0L, 53L),
`[1000,1000000]` = c(20L, 0L), `[3000,1000000]` = c(0L, 0L
), Sum_bin = c(177, 73), strata = list(c(0, 25, 100, 250,
500, 1000, 1e+06), c(0, 25, 100, 250, 500, 1e+06))), row.names = c(NA,
-2L), class = c("data.table", "data.frame"))
library(dplyr)
dat_in_two <- structure(list(rn = c("Type_A", "Type_B"
), `[0,25) East` = c(5L, 0L), `[25,50) South` = c(0L, 0L), `[25,100) West` = c(38L,
3L), `[50,100) North` = c(0L, 0L), `[100,250) East` = c(43L, 5L), `[100,500) South` = c(0L,
0L), `[250,500) South` = c(27L, 12L), `[500,1000) South` = c(44L, 0L), `[1000,1500) South` = c(0L,
0L), `[1500,3000) South` = c(0L, 0L), `[500,1000000] East` = c(0L, 53L),
`[1000,1000000] South` = c(20L, 0L), `[3000,1000000] South` = c(0L, 0L
), Sum_bin = c(177, 73), strata = list(c(0, 25, 100, 250,
500, 1000, 1e+06), c(0, 25, 100, 250, 500, 1e+06))), row.names = c(NA,
-2L), class = c("data.table", "data.frame"))
Issue
I would like to adapt this piece of code below (which extracts the numbers from the strata
column and creates two new columns from it):
dat_in_one %>%
pivot_longer(-c(rn, strata)) %>%
extract(name, c('lower', 'upper'), '(\\d+),(\\d+)', convert = TRUE)
# A tibble: 28 x 5
rn strata lower upper value
<chr> <list> <int> <int> <dbl>
1 Type_A <dbl [7]> 0 25 5
2 Type_A <dbl [7]> 25 50 0
3 Type_A <dbl [7]> 25 100 38
4 Type_A <dbl [7]> 50 100 0
5 Type_A <dbl [7]> 100 250 43
6 Type_A <dbl [7]> 100 500 0
# ... with 22 more rows
It currently ignores the terms East
, South
etc in dat_in_two
. I would like to try to adapt this code, to work on both dat_in_one
and dat_in_two
, where for dat_in_two
it creates a third column. I tried to do, but it does not work at all.
dat_in_two %>%
pivot_longer(-c(rn, strata)) %>%
extract(name, c('lower', 'upper', 'the_rest'), '(\\d+),(\\d+),(\\w+)', convert = TRUE)
Desired outcome for dat_in_one
# A tibble: 28 x 5
rn strata lower upper value
<chr> <list> <int> <int> <dbl>
1 Type_A <dbl [7]> 0 25 5
2 Type_A <dbl [7]> 25 50 0
3 Type_A <dbl [7]> 25 100 38
4 Type_A <dbl [7]> 50 100 0
5 Type_A <dbl [7]> 100 250 43
6 Type_A <dbl [7]> 100 500 0
# ... with 22 more rows
Desired outcome for dat_in_two
# A tibble: 28 x 5
rn strata lower upper rest value
<chr> <list> <int> <int> <char> <dbl>
1 Type_A <dbl [7]> 0 25 East 5
2 Type_A <dbl [7]> 25 50 South 0
3 Type_A <dbl [7]> 25 100 West 38
4 Type_A <dbl [7]> 50 100 North 0
5 Type_A <dbl [7]> 100 250 East 43
6 Type_A <dbl [7]> 100 500 South 0
# ... with 22 more rows
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
怎么样:
在2022-05-04创建的 reprex package (v2.0.1)< /sup>
How about this:
Created on 2022-05-04 by the reprex package (v2.0.1)