有条件地复制数据帧中的行 [英] conditionally duplicating rows in a data frame

查看:56
本文介绍了有条件地复制数据帧中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的数据集的一个示例:

This is a sample of my data set:

   day city count
1   1    A    50
2   2    A   100
3   2    B   110
4   2    C    90

以下是复制代码:

  df <- data.frame(
    day = c(1,2,2,2),
    city = c("A","A","B","C"),
    count = c(50,100,110,90)
    )

您会看到,第一天缺少城市B和C的计数数据,我想做的是使用城市A的计数作为其他两个城市的估计值.因此,所需的输出将是:

As you could see, the count data is missing for city B and C on the day 1. What I want to do is to use city A's count as an estimate for the other two cities. So the desired output would be:

   day city count
1   1    A    50
2   1    B    50
3   1    C    50
4   2    A   100
5   2    B   110
6   2    C    90

我可以想出一个for循环来做到这一点,但是我觉得应该有一种更简单的方法来做到这一点.我的想法是计算每天的观察数,然后对观察数小于数据集中的城市数的日子进行计数,我将复制该行以完成该天的数据.还有更好的主意吗?还是更有效的for循环?谢谢.

I could come up with a for loop to do it, but I feel like there should be an easier way of doing it. My idea is to count the number of observations for each day, and then for the days that the number of observations is less than the number of cities in the data set, I would replicate the row to complete the data for that day. Any better ideas? or a more efficient for-loop? Thanks.

推荐答案

使用 dplyr tidyr ,我们可以做到:

With dplyr and tidyr, we can do:

library(dplyr)
library(tidyr)

df %>% 
  expand(day, city) %>% 
  left_join(df) %>% 
  group_by(day) %>% 
  fill(count, .direction = "up") %>% 
  fill(count, .direction = "down")

或者,我们可以使用latemail的解决方案避免 left_join :

Alternatively, we can avoid the left_join using thelatemail's solution:

df %>% 
  complete(day, city) %>% 
  group_by(day) %>% 
  fill(count, .direction = "up") %>% 
  fill(count, .direction = "down")

两次返回:

# A tibble: 6 x 3
    day city  count
  <dbl> <fct> <dbl>
1    1. A       50.
2    1. B       50.
3    1. C       50.
4    2. A      100.
5    2. B      110.
6    2. C       90.

数据(略作修改以显示同时填充两个方向的 .direction ):

Data (slightly modified to show .direction filling both directions):

df <- data.frame(
  day = c(1,2,2,2),
  city = c("B","A","B","C"),
  count = c(50,100,110,90)
)

这篇关于有条件地复制数据帧中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆