简体   繁体   English

文件中的错误(文件,ifelse(追加,“a”,“w”)):无效的“描述”参数

[英]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.

相关问题 R - “文件错误(文件,ifelse(附加,“a”,“w”)):无法打开连接” - R - "Error in file(file, ifelse(append, "a", "w")) : cannot open the connection" 如何修复文件中的错误(文件,ifelse(附加,“a”,“w”)):无法在 R 笔记本中打开连接 - How to fix Error in file(file, ifelse(append, “a”, “w”)) : cannot open the connection in an R Notebook R闪亮:文件错误(文件,“ rt”):无效的“描述”参数 - R shiny: Error in file(file, “rt”) : invalid 'description' argument 文件(文件,“rt”)中的错误:调用函数时“描述”参数无效 - Error in file(file, "rt") : invalid 'description' argument , when calling the function 如何解决错误“文件错误:无效的&#39;description&#39;参数? - How to resolve error " Error in file : invalid 'description' argument? R Openxlsx 包(版本 4.2.2)-文件错误(描述 = xlsxFile):“描述”参数无效 - R Openxlsx package (version 4.2.2) - Error in file(description = xlsxFile) : invalid 'description' argument for(seq_along(data_file)中的i:file(file,“ rt”)中的错误:无效的&#39;description&#39;参数 - for(i in seq_along(data_file): Error in file(file, “rt”) : invalid 'description' argument gzfile(file,“ rb”)中的错误:无效的&#39;description&#39;参数调用自:gzfile(file,“ rb”) - Error in gzfile(file, “rb”) : invalid 'description' argument Called from: gzfile(file, “rb”) 文件错误(文件,“rt”):complete.cases 程序中的“描述”参数无效 - Error in file(file, "rt") : invalid 'description' argument in complete.cases program 文件错误(文件,“rt”):读取 csv 文件时“描述”参数无效 - Error in file(file, "rt") : invalid 'description' argument when reading csv files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM