Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我正在尝试将一个大的csv按行拆分为单个的.txt文件,在R中的每个.txt文件中都有一个头_R_Csv_Split - Fatal编程技术网

我正在尝试将一个大的csv按行拆分为单个的.txt文件,在R中的每个.txt文件中都有一个头

我正在尝试将一个大的csv按行拆分为单个的.txt文件,在R中的每个.txt文件中都有一个头,r,csv,split,R,Csv,Split,我试图在R studio中为.csv中的每一行创建一个单独的.txt文件。我找到了CSv2Text函数,但我不知道如何编辑它以在每个.txt中保留标题信息 使用以下代码: csv2txt <- function(mydir, labels = 1){ mycsvfile <- list.files(mydir, full.names = TRUE, pattern = "*.CSV|.csv") mycsvdata <- read.csv(mycsv

我试图在R studio中为.csv中的每一行创建一个单独的.txt文件。我找到了CSv2Text函数,但我不知道如何编辑它以在每个.txt中保留标题信息

使用以下代码:

csv2txt <- function(mydir, labels = 1){
  mycsvfile <- list.files(mydir, full.names = TRUE, pattern = "*.CSV|.csv")
  mycsvdata <- read.csv(mycsvfile)
  mytxtsconcat <- apply(mycsvdata[-(1:labels)], 1, paste, collapse=" ")
  mytxtsdf <- data.frame(filename = mycsvdata[,labels], # get the first col for the text file names
                         fulltext = mytxtsconcat) 
  setwd(mydir)
  invisible(lapply(1:nrow(mytxtsdf), function(i) write.table(mytxtsdf[i,2], 
                                                             file = paste0(mytxtsdf[i,1], ".txt"),
                                                             row.names = FALSE, col.names = FALSE,
                                                             quote = FALSE)))
  message(paste0("Your text files can be found in ", getwd()))
}
.csv的顶部有以下标题:

Hotel   Area    Overall Satisfaction for Location   Overall Property Satisfaction   Property Appearance Add'tl Item Working Order   Property Maintenance    Staff Knowledge Staff Interaction   Safety/Security Check In/Out    Invoice Accuracy    Bed Quality
我希望在每一个.txt文件中都有

有人知道我将如何编辑代码来实现这一点吗?或者知道一个函数可以做到这一点


谢谢大家!

如果其他人也有类似的问题,下面是我最后要做的

我将标题强制放入col.names中,这并不理想,因为它们在.txt中没有完全对齐。但是我的解决方案是在元素之间添加|以便在excel中打开.txt,由自定义分隔符分隔,然后您将得到正确对齐的列

代码如下

# Make the .csv into separate .txt files
csv2txt <- function(mydir, labels = 1){
  
  # Get the names of all the CSV file
  mycsvfile <- list.files(mydir, full.names = TRUE, pattern = "*.CSV|.csv")
  
  # Read the actual contexts of the text files into R and rearrange a little.
  
  # create a list of dataframes containing the text
  mycsvdata <- read.csv(mycsvfile)
  
  # combine all except the first column together into
  # one long character string for each row
  mytxtsconcat <- apply(mycsvdata[-(1:labels)], 1, paste, collapse=" | ")
  
  # make a dataframe with the file names and texts
  mytxtsdf <- data.frame(filename = mycsvdata[,labels], # get the first col for the text file names
                         fulltext = mytxtsconcat)
  
  # Now write one text file for each row of the csv
  # use 'invisible' so we don't see anything in the console
  
  setwd(mydir)
  invisible(lapply(1:nrow(mytxtsdf), function(i) write.table(mytxtsdf[i,2], 
                                                             file = paste0(mytxtsdf[i,1], ".txt"),
                                                             row.names = FALSE, col.names = " HOTEL (Q15 1) | METRO AREA STATE (Q10 1)  | Overall Location Satisfaction | Overall Property Satisfaction | Property Appearance | Add'tl Item Working Order | Property Maintenance | Staff Knowledge | Staff Interaction | Safety/Security | Check In/Out | Invoice Accuracy | Bed Quality",
                                                             quote = FALSE)))
  
  # now check your folder to see the txt files
  message(paste0("Your text files can be found in ", getwd()))
}
#将.csv转换成单独的.txt文件

CSv2Text您确定您的csv的标题行没有逗号分隔吗?@AllanCameron,你好,Allan,是的,它在那里。我认为是col.names=FALSE使其不出现,但我尝试使其为真,然后它在顶部给了我一个x,因为我想这需要额外的代码来连接,就像我不知道如何编写的其他行一样。
# Make the .csv into separate .txt files
csv2txt <- function(mydir, labels = 1){
  
  # Get the names of all the CSV file
  mycsvfile <- list.files(mydir, full.names = TRUE, pattern = "*.CSV|.csv")
  
  # Read the actual contexts of the text files into R and rearrange a little.
  
  # create a list of dataframes containing the text
  mycsvdata <- read.csv(mycsvfile)
  
  # combine all except the first column together into
  # one long character string for each row
  mytxtsconcat <- apply(mycsvdata[-(1:labels)], 1, paste, collapse=" | ")
  
  # make a dataframe with the file names and texts
  mytxtsdf <- data.frame(filename = mycsvdata[,labels], # get the first col for the text file names
                         fulltext = mytxtsconcat)
  
  # Now write one text file for each row of the csv
  # use 'invisible' so we don't see anything in the console
  
  setwd(mydir)
  invisible(lapply(1:nrow(mytxtsdf), function(i) write.table(mytxtsdf[i,2], 
                                                             file = paste0(mytxtsdf[i,1], ".txt"),
                                                             row.names = FALSE, col.names = " HOTEL (Q15 1) | METRO AREA STATE (Q10 1)  | Overall Location Satisfaction | Overall Property Satisfaction | Property Appearance | Add'tl Item Working Order | Property Maintenance | Staff Knowledge | Staff Interaction | Safety/Security | Check In/Out | Invoice Accuracy | Bed Quality",
                                                             quote = FALSE)))
  
  # now check your folder to see the txt files
  message(paste0("Your text files can be found in ", getwd()))
}