[英]Error in file(file, ifelse(append, “a”, “w”)) : invalid 'description' argument
I am trying to create a function that takes a data frame of, at least, 100 variables and slice it so I can end up with multiple dfs that always contains the first 5 variables and then 10 of the remaining variables and then write the generated slice.我正在尝试创建一个 function,它需要一个至少包含 100 个变量的数据帧并将其切片,这样我就可以得到多个 dfs,这些 dfs 始终包含前 5 个变量,然后是其余变量中的 10 个,然后写入生成的切片. Example of what I want to automate:
我想要自动化的示例:
First_slice <- df1[,c(1:5, 6:15)]
Second_slice <- df1[,c(1:5, 16:25)]
My function我的 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)
In order to identify the mistake I added several 'print' statements and I found out that the problem is when I want to write each slice into a.csv.为了识别错误,我添加了几个“打印”语句,我发现问题出在我想将每个切片写入 a.csv 时。
I have not found an answer to solve this.我还没有找到解决这个问题的答案。 And interestingly, when I have the write statement out it works.
有趣的是,当我写出 write 语句时,它就起作用了。
Error message错误信息
Error in file(file, ifelse(append, "a", "w")): invalid 'description' argument文件中的错误(文件,ifelse(附加,“a”,“w”)):无效的“描述”参数
Okey, never mind, I realized that the variable 'file_name' is a character vector which contains all the names of my slices.好吧,没关系,我意识到变量 'file_name' 是一个字符向量,其中包含我切片的所有名称。 To solve this I used a for loop.
为了解决这个问题,我使用了一个 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.