[英]R - fail to store multiple output from for loop in vector or data frame
[英]R store the for loop output in vector
我是R初學者,以下是我的代碼:
complete <- function(directory, id = 1:332) {
# Read through all the csv data file
for (i in id) {
i <- sprintf("%03d", as.numeric(i))
data <- read.csv(paste(directory, "/", i, ".csv", sep =""))
good <- complete.cases(data) # Eliminating the NA rows
cases <- sum(good == TRUE) # add complete value
}
data.frame(id = id, nobs = cases )
}
當我打印輸出
id nobs
1 1 402
2 2 402
3 3 402
4 4 402
5 5 402 (incorrect)
如果我只是打印案件
[1] 117
[1] 1041
[1] 243
[1] 474
[1] 402
所以正確的輸出應該是
id nobs
1 1 117
2 2 1041
3 3 243
4 4 474
5 5 402
我意識到它只取(case)的最后一個值。
我的問題是如何將(case)輸出存儲到向量中,所以當我調用data.frame函數時,它將返回正確的輸出。
謝謝
如果id是一個數字向量,那么這應該可以完成這項工作(因為你沒有提供任何可重復的例子,所以未經測試!)
否則你應該for(i in seq_along(id))
使用for(i in seq_along(id))
和id[i]
。
complete <- function(directory, id = 1:332) {
cases <- NULL
# Read through all the csv data file
for (i in id) {
i <- sprintf("%03d", as.numeric(i))
data <- read.csv(paste(directory, "/", i, ".csv", sep =""))
good <- complete.cases(data) # Eliminating the NA rows
cases[i] <- sum(good == TRUE) # add complete value
}
data.frame(id = id, nobs = cases )
}
這是一項更有效的任務:
complete <- function(directory, id = 1:332) {
filenames <- file.path(directory, paste0(sprintf("%03d", id), ".csv"))
data.frame(id = id,
nobs = sapply(filenames, function(x)
sum(complete.cases(read.csv(x)))))
}
complete <- function(directory ,id = 1:332){
folder = directory
df_total = data.frame()
for (x in id){
filenames <- sprintf("%03d.csv", x)
filenames <- paste(folder,filenames,sep="\\")
df <- do.call(rbind,lapply(filenames,read.csv, header=TRUE))
my_vector <- sum(complete.cases(enter the column for which you want))
df1 <- data.frame(id=x,nobs=my_vector)
df_total <- rbind(df_total,df1)
}
df_total
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.