【问题标题】:How to loop through a list and create a data frame如何遍历列表并创建数据框
【发布时间】:2021-03-18 11:48:31
【问题描述】:

提前谢谢你。

我有一个来自 list.files 的文件路径列表。我正在尝试调用每个文件并标准化列数,以便我可以将每个文件绑定到一个大 DF 中。

Team 是我的代码(注意 Add_X_Col 是一个标准化列数的函数):

x1<-read.csv(Team[1],row.names=1) #Reads the csv from the first file path
y<-ncol(x1) #creates object with number of columns
#If statement to standarise number of columns so can bind
if (y=="37"){ 
  x1<-Add_5_Col(x1)
} else if (y=="38"){
  x1<-Add_4_Col(x1)
} else if (y=="39"){
  x1<-Add_3_Col(x1)
} 
Team_Split<-x1 #Creates a new DF called Team_Split 
Team<-Team[-1];#Removes the file path from the list

第二部分 - 我必须对定向文件路径中的所有文件重复此 ~275 次

x1<-read.csv(Team[1],row.names=1) #Reads the csv from the first file path
y<-ncol(x1) #creates object with number of columns
#If statement to standarise number of columns so can bind
if (y=="37"){ 
  x1<-Add_5_Col(x1)
} else if (y=="38"){
  x1<-Add_4_Col(x1)
} else if (y=="39"){
  x1<-Add_3_Col(x1)
} 
Team_Split<-rbind(Team_Split,x1) #Binds to already made Team_Split DF 
Team<-Team[-1]; 

我的预期输出将是一个大型数据框,将文件路径中的每个文件作为数据框读取并将它们绑定在一起。

我不熟悉循环和使用应用包;我试图了解他们在这里使用它们,但无济于事!任何帮助将不胜感激。

按要求编辑。

Add_3_Col <- function(x){
  names(x1)[names(x1) == "X60"] <- "X60.1"
  x1$X60<- 0
  x1$X105<- 0
  x1$X110<- 0
  x1<-x1[c(1:12,40,13:24,41,42,25:39)]
  return(x1)
}
Add_5_Col <- function(x){
  names(x1)[names(x1) == "X55"] <- "X55.1"
  names(x1)[names(x1) == "X60"] <- "X60.1"
  x1$X55<- 0
  x1$X60<- 0
  x1$X100<- 0
  x1$X105<- 0
  x1$X110<- 0
  x1<-x1[c(1:11,38,39,12:22,40,41,42,23:37)] #re-arrnages to put the columns in the correct place.
  return(x1)
}
Add_4_Col <- function(x){
  names(x1)[names(x1) == "X55"] <- "X55.1"
  names(x1)[names(x1) == "X60"] <- "X60.1"
  x1$X55<- 0
  x1$X60<- 0
  x1$X105<- 0
  x1$X110<- 0
  x1<-x1[c(1:11,39,40,12:23,41,42,24:38)]
  return(x1)
}

【问题讨论】:

  • 你能添加ADD_X_COLS函数的代码吗?
  • @iamericfletcher 查看编辑

标签: r dplyr apply


【解决方案1】:

这对我来说适用于一堆带有模拟数据的 CSV 文件。

Team <- list.files("c:\\Test\\Teams\\", full.names=TRUE)

Team_Split <- data.frame()
print(Team)

for (Team_File in Team) {
  xl <-
    read.csv(Team_File) #Reads the csv from the first file path
  y <- ncol(x1) #creates object with number of columns
  #If statement to standarise number of columns so can bind
  if (y == "37") {
    x1 <- Add_5_Col(x1)
  } else if (y == "38") {
    x1 <- Add_4_Col(x1)
  } else if (y == "39") {
    x1 <- Add_3_Col(x1)
  }
  # Sets Team_Split to xl if it's the first set of data 
  # or binds Team_Split and xl
  print(xl)
  
  if (nrow(Team_Split) == 0) {
    Team_Split <- xl 
  } else {
    Team_Split <- rbind(Team_Split, xl)
  }
}

print(Team_Split)

【讨论】:

  • 感谢您的回答@norie。您的建议会创建文件名/路径列表的输出。查看我对原始帖子的编辑以查看我的预期输出。
  • 我的回答中有一些拼写错误,但它们会导致错误而不是您描述的行为。我已经更新了我的代码以修复错别字
  • 我仍然相信有一些错字,即 xl 我假设应该是 x1。我将所有 xl 都更改为 x1,但是仍然存在一些问题。您的脚本已使用前两个文件路径来创建 DF,但它不会循环遍历列表中剩余的 273 个文件。
【解决方案2】:

My intended output would be a large dataframe, reading each file in the file path as a dataframe and binding them together.

取决于文件在您的目录中的位置:

library(readr)
library(dplyr)

files <- dir("data/", pattern = "\\.csv$", full.names = TRUE)

df2_list <- vector("list", length(files))

names(df2_list) <- files

for (fname in files) {
  df2_list[[fname]] <- read_csv(fname)
}

df2 <- bind_rows(df2_list)

然后是这样的?

library(dplyr)
df2 %>% summarise_each(funs(na.omit(.)))

此解决方案将 3 个独立的 .csv 文件与我机器上不同数量的列组合在一起,所有这些文件都位于我的工作目录中的 data 文件夹中。

df2 %&gt;% summarise_each(funs(na.omit(.))) 代表加入后生成的NAs。

【讨论】:

    猜你喜欢
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-05
    • 2023-02-04
    • 2023-01-19
    相关资源
    最近更新 更多