Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read data separated with two colons in R [duplicate]

Tags:

r

separator

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?

like image 332
midtownguru Avatar asked Nov 09 '13 21:11

midtownguru


1 Answers

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")
like image 180
Michele Avatar answered Oct 12 '22 09:10

Michele