0

我想根据日期列 A 计算列 D。列 D 应表示按列 B 分组的观察数。

编辑:下面的假数据

 data <- structure(list(date = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 9L, 
    10L, 11L, 12L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("1/1/2015", 
    "1/2/2015", "1/3/2015", "1/4/2015", "1/5/2015", "1/6/2015", "5/10/2015", 
    "5/11/2015", "5/6/2015", "5/7/2015", "5/8/2015", "5/9/2015"), class = "factor"), 
        Country = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
        2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("A", "B", 
        "C"), class = "factor"), Value = c(215630672L, 1650864L, 
        124017368L, 128073224L, 97393448L, 128832128L, 14533968L, 
        46202296L, 214383720L, 243346080L, 85127128L, 115676688L, 
        79694024L, 109398680L, 235562856L, 235473648L, 158246712L, 
        185424928L), Number.of.Observations.So.Far = c(1L, 2L, 3L, 
        4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L
        )), class = "data.frame", row.names = c(NA, -18L))

R 中的什么函数会像这样创建 D 列?

在此处输入图像描述

4

1 回答 1

0

我们可以按“国家”分组并创建序列列row_number()

library(dplyr)
df1  %>%
   group_by(Country) %>%
   mutate(NumberOfObs = row_number())

或与base R

df1$NumberOfObs <- with(df1, ave(seq_along(Country), Country, FUN = seq_along))

或与table

df1$NumberOfObs <- sequence(table(df1$Country))

或在data.table

library(data.table)
setDT(df1)[, NumberOfObs := rowid(Country)][]

数据

df1 <- read.csv('file.csv')
于 2020-01-26T00:58:59.617 回答