gpt4 book ai didi

r - 拆分由数字范围组成的列,并将结果数字用作 R 中的范围值

转载 作者:行者123 更新时间:2023-12-04 09:04:40 27 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Split data frame string column into multiple columns

(15 个回答)



Expand ranges defined by “from” and “to” columns

(9 个回答)


去年关闭。




我的示例数据框如下所示:

structure(list(Speed = c("0-20", "21-40", "41-60", "61-80", "81-100"
), SpeedLevel = c(1, 2, 3, 4, 5)), row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame"))
>
我需要添加一个列,该列将包含与第一列“速度”对应的范围内的所有值。
即,我需要在“-”处拆分字符串并给出从最小值到最大值的值范围。
例如,在 Speed 列的第一行中,我们有 '0-20',因此在拆分范围后,范围将是从 0 到 20 的所有数字。一旦我得到了,我就可以分别使用单独的行或 tidyr 和 dplyr 的 unnest 函数作为显示在下面的预期输出中。
预期输出:
structure(list(Speed = c("0-20", "0-20", "0-20", "0-20", "0-20", 
"0-20", "0-20", "0-20", "0-20", "0-20", "0-20", "0-20", "0-20",
"0-20", "0-20", "0-20", "0-20", "0-20", "0-20", "0-20", "0-20",
"21-40", "21-40", "21-40", "21-40", "21-40", "21-40", "21-40",
"21-40", "21-40", "21-40", "21-40", "21-40", "21-40", "21-40",
"21-40", "21-40", "21-40", "21-40", "21-40", "21-40", "41-60",
"41-60", "41-60", "41-60", "41-60", "41-60", "41-60", "41-60",
"41-60", "41-60", "41-60", "41-60", "41-60", "41-60", "41-60",
"41-60", "41-60", "41-60", "41-60", "41-60", "61-80", "61-80",
"61-80", "61-80", "61-80", "61-80", "61-80", "61-80", "61-80",
"61-80", "61-80", "61-80", "61-80", "61-80", "61-80", "61-80",
"61-80", "61-80", "61-80", "61-80", "81-100", "81-100", "81-100",
"81-100", "81-100", "81-100", "81-100", "81-100", "81-100", "81-100",
"81-100", "81-100", "81-100", "81-100", "81-100", "81-100", "81-100",
"81-100", "81-100", "81-100"), SpeedLevel = c(1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5), ActualSpeed = c(0, 1, 2,
3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
100)), row.names = c(NA, -101L), class = c("tbl_df", "tbl", "data.frame"
))
>
对于字符串拆分,我使用“strsplit”函数,但不确定是否可以在这里使用。有人可以让我知道如何拆分“速度”列并将两个结果数字用作范围值。

最佳答案

我们可以使用 separate 将“速度”分成两列,然后创建一个序列 list基于 'start'、'end' 和 unnest 的值的列list柱子

library(dplyr)
library(tidyr)
library(purrr)
df1 %>%
separate(Speed, into = c('start', 'end'), remove = FALSE, convert = TRUE) %>%
mutate(AcutalSpeed = map2(start, end, `:`), start = NULL, end = NULL) %>%
unnest(c(AcutalSpeed))
# A tibble: 101 x 3
# Speed SpeedLevel AcutalSpeed
# <chr> <dbl> <int>
# 1 0-20 1 0
# 2 0-20 1 1
# 3 0-20 1 2
# 4 0-20 1 3
# 5 0-20 1 4
# 6 0-20 1 5
# 7 0-20 1 6
# 8 0-20 1 7
# 9 0-20 1 8
#10 0-20 1 9
# … with 91 more rows

关于r - 拆分由数字范围组成的列,并将结果数字用作 R 中的范围值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63475394/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com