r - 使用 cbind 一起添加到列表中
问题描述
该程序有效,因为我使用 <<- 运算符使变量 inisde lapply 全局化。但是,它不适用于真实程序中的真实文件。这些是带有命名列的 .tsv 文件。当我运行真正的程序时得到的答案是:错误:(从警告转换)错误:(从警告转换)错误:参数意味着不同的行数:3455、4319。这可能是什么原因?
lc <- list("test.txt", "test.txt", "test.txt", "test.txt")
lc1 <- list("test.txt", "test.txt", "test.txt")
lc2 <- list("test.txt", "test.txt")
#list of lists. The lists contain file names
lc <- list(lc, lc1, lc2)
#new names for the three lists in the list of lists
new_dataFns <- list("name1", "name2", "name3")
file_paths <- NULL
new_path <- NULL
#add the file names to the path and read and merge the contents of each list in the list of lists
lapply(
lc,
function(lc) {
filenames <- file.path(getwd(), lc)
dataList <<- lapply(filenames, function (lc) read.table(file=lc, header=TRUE))
dataList <<- lapply(dataList, function(dataList) {merge(as.data.frame(dataList),as.data.frame(dataList))})
}
)
#add the new name of the file to the path total will be 3 paths/fille_newname.tsv.
lapply(new_dataFns, function(new_dataFns) {new_path <<- file.path(getwd(), new_dataFns)})
print(new_path)
print(dataList)
finalFiles <- merge(as.data.frame(dataList), as.data.frame(new_path))
print(finalFiles)
解决方案
我通过编写不同类型的代码找到了解决问题的方法。请看下文。函数的输入由应用程序输入小部件提供
glyCount1 <- function(answer = NULL, fileChoice = NULL, combination = NULL, enteredValue = NULL, nameList) {
lc = nameList
new_dataFns <- gsub(" ", "", nameList)
first_path <- NULL
new_path <- NULL
old_path <- NULL
file_content <- NULL
for(i in 1:length(lc)){
for(j in 1:length(lc[[i]])){
if(!is.null(lc[[i]])){
first_path[[j]]<- paste(getwd(), "/", lc[[i]][j], sep = "")
tryCatch(file_content[[j]] <- read.csv(file = first_path[[i]], header = TRUE, sep = ","), error = function(e) NULL)
old_path[[j]] <- paste(getwd(), "/", i, ".csv", sep = "")
write.table(file_content[[j]], file = old_path[[j]], append = TRUE, col.names = FALSE)
}
}
}
}
推荐阅读
- swift - WKWebView 不调用导航委托方法
- pandas - PyODBC+Pandas+Read_SQL:错误:游标的连接已关闭
- java - 如何使用 Maven 将 Clojure 依赖项包含到 Java 项目中
- python - 无法使用 python 库:'paramiko' 与 AWS Lambda
- html - CSS 响应式分页
- javascript - 如何防止登录用户访问登录页面?
- python - 在这个排序算法中,元组的返回语句中的 0 索引去了哪里?
- python - Python 编程{副作用}
- reactjs - 使用 Typescript 不允许 React createContext(null)?
- javascript - 如何从数组中选择随机颜色?