【发布时间】: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 查看编辑