특정 폴더에서 모두 동일한 구조의 CSV 파일을 읽습니다. 또한 dataFrame에 특정 값을 추가하는 함수를 만들었습니다.특정 폴더의 모든 csv 파일에 함수 적용
"폴더 읽기"- 부분을 만들었고 함수도 만들었습니다. 그러나 이제는이 두 가지를 서로 연결해야합니다.
다음내 코드입니다 :
addValue <- function(valueToAdd, df.file, writterPath) {
df.file$result <- df.file$Value + valueToAdd
x <- x + 1
df.file <- as.data.frame(do.call(cbind, df.file))
fullFilePath <- paste(writterPath, x , "myFile.csv", sep="")
write.csv(as.data.frame(df.file), fullFilePath)
}
#1.reading R files
path <- "C:/Users/RFiles/files/"
files <- list.files(path=path, pattern="*.csv")
for(file in files)
{
perpos <- which(strsplit(file, "")[[1]]==".")
assign(
gsub(" ","",substr(file, 1, perpos-1)),
read.csv(paste(path,file,sep="")))
}
#2.appyling function
writterPath <- "C:/Users/RFiles/files/results/"
addValue(2, sys, writterPath)
어떻게 내 #1.reading R files
구조에 addValue()
기능을 적용하기 위해 내가하는 데 문제가있는 곳은? 어떤 추천?
답변 해 주셔서 감사합니다.
UPDATE가
이 예제 코드를하려고 할 때 내가 얻을 :
+ }
+ ## If you really need to change filenames with numbers,
+ newfname <- file.path(npath, paste0(x, basename(fname)))
+ ## otherwise just use `file.path(npath, basename(fname))`.
+
+ ## (4) Write back to a different file location:
+ write.csv(newdat, file = newfname, row.names = FALSE)
+ }
Error in `$<-.data.frame`(`*tmp*`, "results", value = numeric(0)) :
replacement has 0 rows, data has 11
어떤 제안?
어떤 문제가 있습니까? ('list.files()'에 대한 호출에서'full.names = TRUE'가 필요합니까?) – r2evans
@ r2evans Thx 답장을 보내주십시오! 내 문제는 내''Rread' 파일 구조에'addValue()'함수를 적용하는 법을 모른다는 것입니다. Atm이 두 가지는 서로 완전히 별개입니다 ... – mrquad