我在 SQL Server 中有一个表,我正在尝试向其中添加数据。在添加数据之前,我想删除所有现有记录,但我不想删除该表并重新创建它,因为它在 SQL Server 中创建了我想保留的索引。
我有什么选择来使用 r 来完成这个?
我正在尝试创建按不同列分组的新列,但我不确定我这样做的方式是否是使用 group_by 的最佳方式。我想知道是否有办法让 group_by 排队?
我知道它可以使用 data.table 包来完成,其中语法类型为 DT[i,j, by]。
但由于这是使用 tidyverse 的较大代码中的一小部分,并且按原样运行良好,我只是不想偏离这一点。
## Creating Sample Data Frame
state <- rep(c("OH", "IL", "IN", "PA", "KY"),10)
county <- sample(LETTERS[1:5], 50, replace = T) %>% str_c(state,sep = "-")
customers <- sample.int(50:100,50)
sales <- sample.int(500:5000,50)
df <- bind_cols(data.frame(state, county,customers,sales))
## workflow
df2 <- df %>%
group_by(state) %>%
mutate(customerInState = sum(customers),
saleInState = sum(sales)) %>%
ungroup %>%
group_by(county) %>%
mutate(customerInCounty = sum(customers),
saleInCounty = sum(sales)) %>%
ungroup %>%
mutate(salePerCountyPercent = saleInCounty/saleInState,
customerPerCountyPercent = customerInCounty/customerInState) …Run Code Online (Sandbox Code Playgroud)