【问题标题】:For Loop in R to read in dataR中的for循环读取数据
【发布时间】:2021-03-27 21:34:40
【问题描述】:

我有以下代码,我想从中创建一个 for 循环。我只需要更改所有行的年份数(1996-2019 年)。以下是我的代码:

# loading health data
health_data_1996 <- read.csv("1996-Annual.csv")
#delete data which is not needed
health_data_1996 <- health_data_1996[!(health_data_1996$Measure.Name != "Unemployment Rate, Annual" & 
health_data_1996$Measure.Name != "High School Graduation"),]
health_data_1996 <- health_data_1996[,-c(1,2,5,7:11)]
#rename value column
colnames(health_data_1996)[3] <- "1996"

谁能告诉我如何用这个来做一个 for 循环?

非常感谢您的帮助。

【问题讨论】:

  • 请提供一个(小)可重现的数据示例,其列与真实数据相同。你每年有一个 csv 吗?您想将它们组合成一个数据集吗?
  • 对不起,我对 R 和 Stackoverflow 比较陌生。是的,我每年有一个 csv,我不想合并数据集,我只想读入它们。有以下 11 列:Edition,Report.Type,Measure.Name,State.Name,Rank,Value,分数、Lower.CI、Upper.CI、Source、Source.Year

标签: r


【解决方案1】:

由于您只想读取数据集而不是合并它们,因此我建议以下内容。我在这里假设您所有的 CSV 文件都具有相同的名称结构。

# create a vector with all the years
years <- 1996:2019

# apply the desired function on every value in years consecutively
all_data <- lapply(years, function(y) {
  df <- read.csv(paste0(y, "-Annual.csv"))
  
  df <- df[df$Measure.Name == "Unemployment Rate, Annual" |
      df$Measure.Name == "High School Graduation", ]
  
  df <- df[, -c(1, 2, 5, 7:11)]
  
  colnames(df)[3] <- y
  
  df
})

这将为您提供一个名为 list 的元素,其中每个元素都是给定年份的数据集。因此,例如,如果您想要 2019 年的数据,您应该能够使用 all_data[["2019"]] 检索它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多