简体   繁体   English

R 软件,读取.csv,多个分隔符

[英]R software, read.csv, multiple separators

Does anyone know a way to read a csv file in R with multiple separators?有谁知道使用多个分隔符读取 R 中的 csv 文件的方法?

a<-read.csv("C:/Users/User/Desktop/file.csv", sep=",", header=FALSE)

Here, I have the following dataset (txt/csv file) separated by commas and spaces:在这里,我有以下数据集(txt/csv 文件),用逗号和空格分隔:

5.006,84.698  
4.604,87.725  7.250,88.392  
6.668,91.556  
5.927,95.440  
4.953,99.695  7.387,100.489  
6.466,104.447  
5.599,107.548  
4.053,111.411  7.440,112.892  
6.096,116.417  
4.805,119.031  7.546,120.671  
6.149,123.793  
4.307,127.201  7.461,129.974  
5.493,132.853  7.641,135.393  

and I want it to be read as a table with four columns, like this:我希望它被解读为一个有四列的表格,如下所示:

72 5.006  84.698    NA      NA  
73 4.604  87.725 7.250  88.392  
74 6.668  91.556    NA      NA  
75 5.927  95.440    NA      NA  
76 4.953  99.695 7.387 100.489  
77 6.466 104.447    NA      NA  
78 5.599 107.548    NA      NA  
79 4.053 111.411 7.440 112.892  
80 6.096 116.417    NA      NA   
81 4.805 119.031 7.546 120.671  
82 6.149 123.793    NA      NA  
83 4.307 127.201 7.461 129.974  
84 5.493 132.853 7.641 135.393  

Do you know the possible way to read it that way in R?您知道在 R 中以这种方式阅读它的可能方式吗?

We can try using readLines() to read each line as a string.我们可以尝试使用readLines()将每一行读取为字符串。 Then, we can split on multiple separators and roll up into a data frame.然后,我们可以拆分多个分隔符并汇总成一个数据框。

file <- "C:/Users/User/Desktop/file.csv"
txt <- readLines(file, sep = ""))
y <- strsplit(txt, "[, ]+")
z <- lapply(y,function(x){as.data.frame(t(as.numeric(x)))})
df <- do.call(rbind.fill, z)
df

You could open the file in any text editor (notepad or something similar) and make the separators common across the file.您可以在任何文本编辑器(记事本或类似的东西)中打开文件,并使分隔符在文件中通用。 You can either replace ',' with spaces or vice-versa using Find and Replace all and save the file.您可以使用空格替换',' ,反之亦然,使用全部查找和替换并保存文件。

Once you do that you can use read.csv with this new separator.一旦你这样做了,你可以使用read.csv这个新的分隔符。

a <- read.csv("C:/Users/User/Desktop/file.csv", sep= " ", header=FALSE, fill = TRUE)

One option is to use Excel.一种选择是使用 Excel。 You can choose multiple separators (delimiters) during the import stage (Wizard step 2).您可以在导入阶段(向导步骤 2)选择多个分隔符(定界符)。 Comma and space are one of the default choices but you can choose other characters too.逗号和空格是默认选项之一,但您也可以选择其他字符。

Then import the excel file using one of many user-contributed packages, for example, readxl , or save as text and use read.csv / read.table .然后使用许多用户提供的软件包之一导入 excel 文件,例如readxl ,或另存为文本并使用read.csv / read.table

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM