我正在尝试使用for循环的结果填充数据帧,但我遗漏了一些东西。我已经查看了过去有关此问题的问题,但我无法理解解决方案,所以......
虚构的数据:
df <- data.frame(RA = c(rep("14005", 3), rep("14158", 3), rep("15458", 2), rep("15302", 2)),
Level = c(rep("Grad", 6), rep("Undergrad", 4)),
EntryYear = c(rep(2014, 6), rep(2015, 4)),
ExitYear = c(rep(2016, 3), rep(2017, 3), rep(2018, 4)))
我想用特定dplyr管道的结果填充数据框:
df %>%
filter(Level == "Grad", EntryYear <= year, ExitYear >= year) %>%
distinct(RA) %>%
summarise(year = n())
哪一年是我感兴趣的特定年份(根据我的原始数据,从2010年到2017年)。上面的公式是大致计算在给定年份中注册的学生人数。 [我将使用另外两个公式来计算毕业生和新生的数量,所以我还有两个行/列]。和:
start.year <- 2010
end.year <- 2017
所以,我做了这个for循环,看它是否有效:
for (year in start.year:end.year){
mat <- df %>%
filter(Level == "Grad", EntryYear <= year, ExitYear >= year) %>%
distinct(RA) %>%
summarise(year= n())
print(mat)
}
它打印出我想要的内容,但后来我无法将其写入数据帧......预期的结果是一个包含7行的数据帧。最终结果是一个包含7行和3列的数据框(我将应用的其他两个公式/管道)。
由于您已经在使用dplyr
,因此很容易使用purrr
为您合并data.frames
library(purrr)
map_df(start.year:end.year, function(year) {
mat <- df %>%
filter(Level == "Grad" & EntryYear <= year & ExitYear >= year) %>%
distinct(RA) %>%
summarise(year= n())
})
只需在最终代码中添加以下两行:
new_df <- data.frame(). # <- this one
for (year in start.year:end.year){
mat <- df %>%
filter(Level == "Grad", EntryYear <= year, ExitYear >= year) %>%
distinct(RA) %>%
summarise(year= n())
new_df <- rbind(new_df, mat) # <- this one
}
for (year in start.year:end.year){
mat <- df %>%
filter(Level == "Grad", EntryYear <= year, ExitYear >= year) %>%
distinct(RA) %>%
summarise(year= n())
print(mat)
if (year<2011){
final <- as.data.frame(mat)
}
else{
final <- rbind(final,as.data.frame(mat))
}
}
# load packages
library(plyr)
library(dplyr)
# Filter function
filterdf<-function(year, level){
mat <- df %>%
filter(Level == level, EntryYear <= year, ExitYear >= year) %>%
distinct(RA) %>%
summarise(year= n())
colnames(mat)<-paste0(level, "_number")
return(mat)
}
# Create your input conditions
input<-as.data.frame(seq(2010,2017))
colnames(input)<-"year"
input$level<-"Grad"
# Output to a dataframe
output<-mdply(input,filterdf)
这是一个tidyverse
替代方案:
library(tidyverse)
df %>%
filter(Level=="Grad") %>%
mutate(year = map2(EntryYear,ExitYear,~.x:.y)) %>%
unnest(year) %>%
distinct(RA,year) %>%
count(year)
# # A tibble: 4 x 2
# year n
# <int> <int>
# 1 2014 2
# 2 2015 2
# 3 2016 2
# 4 2017 1