![](/img/trans.png)
[英]R - "Error in file(file, ifelse(append, "a", "w")) : cannot open the connection"
[英]Error in file(file, ifelse(append, “a”, “w”)) : invalid 'description' argument
我正在嘗試創建一個 function,它需要一個至少包含 100 個變量的數據幀並將其切片,這樣我就可以得到多個 dfs,這些 dfs 始終包含前 5 個變量,然后是其余變量中的 10 個,然后寫入生成的切片. 我想要自動化的示例:
First_slice <- df1[,c(1:5, 6:15)]
Second_slice <- df1[,c(1:5, 16:25)]
我的 function
Auto.Slice <- function(df, base= 'slice'){
x <- seq(6, 194, 10) #vector with the starts of my slices
#print(x)
#View(df)
One.Slice <- function(df1, x=NULL, base){
#View(df1)
y <- if (x+9 <= ncol(df1)) x+9 else ncol(df1)
#print(y)
df2 <- df1[,c(1:5, x:y)]
#View(df2)
#print(base)
file_name <- paste(base, x,'_', y, '.csv', sep ='')
#print(file_name)
write.table(df2, file_name, row.names = F, sep='\t', col.names = T)
}
One.Slice(df, x, base)
}
Auto.Slice(df = MS)
為了識別錯誤,我添加了幾個“打印”語句,我發現問題出在我想將每個切片寫入 a.csv 時。
我還沒有找到解決這個問題的答案。 有趣的是,當我寫出 write 語句時,它就起作用了。
錯誤信息
文件中的錯誤(文件,ifelse(附加,“a”,“w”)):無效的“描述”參數
好吧,沒關系,我意識到變量 'file_name' 是一個字符向量,其中包含我切片的所有名稱。 為了解決這個問題,我使用了一個 for 循環。
Auto.Slice <- function(df, base= 'slice'){
x <- seq(6, 194, 10) #vector with the starts of my slices
One.Slice<-function(df1, x=NULL, base){
y <- if (x+9 <= ncol(df1)) x+9 else ncol(df1)
df2 <- df1[,c(1:5, x:y)]
file_name <- paste(base, x,'_', y, '.csv', sep ='')
for (i in file_name){
write.table(df2, i, row.names = F, sep='\t', col.names = T)
}
}
One.Slice(df, x, base)
}
Auto.Slice(df = MS)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.