簡體   English   中英

R - “文件錯誤(文件,ifelse(附加,“a”,“w”)):無法打開連接”

[英]R - "Error in file(file, ifelse(append, "a", "w")) : cannot open the connection"

我有這種形式的數據框:

 X1 X2 X3 X4 R290601 WOVEN TWILL 001 6 231 009-1373 *with perforated L3-0,3 152 NA <NA> R481400 THREAD A1282 12 A0399 0091375 PURE SOCK 001 6 072 R282380 SOFTLIN W/FELT 007 6 072 R282480 MICROFIBRE 001 6 F72 R281200 ARTIFICIAL A0638 6 072

我想遍歷行,對於每一行,檢查第一列 (X1) 的名稱並在我的計算機中創建一個具有相同名稱的文件夾,並在該文件夾內創建與它們具有相同名稱的子文件夾相應的列 (X2)、(X3)、(X4)。 當我運行腳本時,我只能看到創建了文件夾R290601 WOVEN TWILL ,其中包含子文件夾001、6231 ,但沒有rest ,我收到此錯誤:

文件錯誤(file, ifelse(append, "a", "w")): 無法打開連接

此外,我在第二行收到此警告:

在 dir.create(paste0(pth, df$X1[i])): cannot create dir 'C:\Users\Dev\Desktop\Joe\009-1373 *with perforated L3-0,3', 原因 'Invalid argument '

我的代碼是這樣的:

 getwd() setwd("C:/Users/Dev/Desktop/Joe") library(xlsx) library(rJava) library(xlsxjars) #get file names f = list.files("./") #read files dat = lapply(f, function(i){ x = read.xlsx(i, sheetIndex = 1, sheetName = NULL, startRow = 24, endRow = NULL, as.data.frame = TRUE, header = FALSE) #return columns with names and colors x = x[, c(1, 2, 3, 4), drop=FALSE] #return the data x }) library(plyr) df1 <- ldply(dat, data.frame) ## convert list into a dataframe #remove NA's complete.cases(df1) x <- df1[complete.cases(df1),] str(x) #show only rows that start with numbers or 1 letter and then numbers df <-df1[grepl("^[0-9]|^[a-zA-Z][0-9].*", df1$X1), ] print(df) pth <- "C:/Users/Dev/Desktop/Joe/" # Iterate within each row of df for(i in 1:nrow(df)){ # Create 1st path dir.create(paste0(pth, df$X1[i])) # Create 2nd and 3rd paths dir.create(paste0(pth, df$X1[i], "/",df$X2[i])) dir.create(paste0(pth, df$X1[i], "/",df$X3[i])) dir.create(paste0(pth, df$X1[i], "/",df$X4[i])) # write data.frame row as txt write.table(df[i, ], file=paste0(pth, df$X1[i], "/", df$X1[i],".txt"), sep=";") }

為什么會出現此錯誤,我如何才能看到所有文件夾及其相應的子文件夾?

library(readxl)
library(tidyverse)
## First read the dataframe taht contains Folder & Sub-folder names
df <- read_excel("C:/Users/Dev/Desktop/Joe/df.xlsx") 


## Special characters like "\ / : * ? " < > |" cann't be present in filename,
## So first remove it from the df for every column.
df <- df %>% 
  mutate(across(everything(), .fns = function(x) gsub('[[:punct:]]','', x))) ## '[[:punct:]]' for the special character


setwd('C:/Users/Dev/Desktop/Joe') ## Replace with your working directory



for(i in 1:nrow(df)){
  
  df_name <- df[i, ] %>% 
    select_if(~ !is.na(.x)) %>% 
    select_if(~ .x != 'NA') ## if NA is character & you don't need it
  
  for(j in 1:ncol(df_name)){
    
    if(!is.na(df_name[,j])){
      if(!dir.exists(as.character(df_name[,j]))){
        dir.create(as.character(df_name[,j]), recursive = TRUE)
        setwd(as.character(df_name[,j])) ## Set wd to the newly created dir
        ## X1 --> X2 --> X3 --> X4
      }
    }
    
  }
  
  setwd('C:/Users/Dev/Desktop/Joe') ## Again go back to the main wd

}

## IF you want the folder like this : X1 --> X2, X3, X4 then 

for(i in 1:nrow(df)){
  
  df_name <- df[i, ] %>% 
    select_if(~ !is.na(.x)) %>% 
    select_if(~ .x != 'NA') ## if NA is character & you don't need it
  
  if(!is.na(df_name[,1])){
    if(!dir.exists(as.character(df_name[,1]))){
      dir.create(as.character(df_name[,1]), recursive = TRUE)
      setwd(as.character(df_name[,1])) ## Set wd to the newly created dir
    }
  }
  
  for(j in 2:ncol(df_name)){
    
    if(!is.na(df_name[,j])){
      if(!dir.exists(as.character(df_name[,j]))){
        dir.create(as.character(df_name[,j]), recursive = TRUE)
      }
    }
    
  }
  
  setwd('C:/Users/Dev/Desktop/Joe') ## Again go back to the main wd
  
}

我遇到了同樣的問題,我只是通過創建一個具有相同第一列數據的 excel 文件(我無法在 csv 中提取)來糾正它。 我無法在 csv 中提取的數據將在此處稱為 dataA,excel 文件 (.xl) 將在此處稱為 dataB。 因此,在 dataB 中,第一列與 dataA 中的第一列相同。 我導入dataB dataB <- read_excel(".....") 我只是將列從dataA一一導入到dataB:dataB$Secondcolumn <- dataA$Secondcolumn dataB$Thirdcolumn <- dataA$Thirdcolumn。 . . 然后: write.csv(dataB, "dataB")

我剛剛遇到了同樣的問題,我意識到這是因為我有它要求在 Excel 中打開的 .csv 文件。關閉文件解決了問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM