I am trying to read data separated with :: (two colons) in R. When I use sep = "::" with read.table function, I get an error message, "sep value must be one byte." I am wondering if I can read this data in R. I did not have any problem with reading this data in Python.

For now, I use sep=":" and then get NA's in columns that were the other ":". So I delete columns of NA's. Is there a way to read data directly by specifying sep = "::" in R?

有帮助吗?

解决方案

Let's say we have:

A::B::C
23::34::56
12::56::87
90::43::74

in a txt file. Then we can do:

lines <- readLines("doublesep.txt")
> lines
[1] "A::B::C"    "23::34::56" "12::56::87" "90::43::74"

lines <- gsub("::", ",", lines)
> lines
[1] "A,B,C"    "23,34,56" "12,56,87" "90,43,74"

Now, you can either write to a file or convert to a data.frame object:

> read.table(text=lines, sep=",", header=T)
   A  B  C
1 23 34 56
2 12 56 87
3 90 43 74

> writeLines(lines, "doubletosingle.csv")
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top