【问题标题】:Duplicating rows n times, where n is a value of a string复制行 n 次,其中 n 是字符串的值
【发布时间】:2021-09-18 00:17:37
【问题描述】:

我有一个数据集,其中列出了各州及其各自的城市,其中一些地方已汇总(不是由我汇总)并归类为 "Other ([count of places])" (e.g. Other (99))。附加到此位置列表的是数字 'count' 值。我想 1.) 找到每个位置的平均计数和 2.) 根据括号内的数字复制这些“其他...”位置以及平均值。示例如下:

set.seed(5)
df <- data.frame(state = c('A','B'), city = c('Other (3)','Other (2)'), count = c('250','50'))

输出:

state city count
A Other (3) 83.333
A Other (3) 83.333
A Other (3) 83.333
B Other (2) 25.000
B Other (2) 25.000

到目前为止,我只能弄清楚如何从括号中提取数字并创建一个平均值:

average = df$count/as.numeric(gsub(".*\\((.*)\\).*", "\\1", df$city))

【问题讨论】:

    标签: r repeat


    【解决方案1】:

    uncount 的选项。用parse_number提取'city'中的数字部分,将'count'除以'n'并用uncount复制行

    library(dplyr)
    library(tidyr)
    df %>%
        mutate(n = readr::parse_number(city), count = as.numeric(count)/n) %>%
        uncount(n)
    

    -输出

    state      city    count
    1     A Other (3) 83.33333
    2     A Other (3) 83.33333
    3     A Other (3) 83.33333
    4     B Other (2) 25.00000
    5     B Other (2) 25.00000
    

    【讨论】:

      【解决方案2】:

      您可以使用以下代码扩展您的示例:

      set.seed(5)
      df <- data.frame(state = c('A','B'), city = c('Other (3)','Other (2)'), count = c('250','50'))
      times <- as.numeric(gsub(".*\\((.*)\\).*", "\\1", df$city))
      df$count <- as.numeric(df$count)/times
      output <- df[rep(seq_along(times),times),]
      

      关键添加是创建输出的行,它使用输入数据帧上的行索引来根据需要重复每一行。

      【讨论】:

        猜你喜欢
        • 2015-05-25
        • 2013-12-27
        • 2019-11-12
        • 2013-07-27
        • 2013-10-12
        • 2018-07-04
        • 2020-04-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多