Евгений Греков Евгений Греков - 4 months ago 9
R Question

How to split columns in data frame by semicolon in R

My question feels to me too obvious, however, I could not find a solution.

a have a data frame like this:

<TICKER>;<PER>;<DATE>;<TIME>;<OPEN>;<HIGH>;<LOW>;<CLOSE>
USD Index;D;20150801;000000;97.199;97.336;97.191;97.192
USD Index;D;20150802;000000;97.226;97.294;97.207;97.257
USD Index;D;20150803;000000;97.255;97.582;97.155;97.499


I need them split in different columns by ; like this:

<TICKER> <PER> <DATE> <TIME> <OPEN> <HIGH> <LOW> <CLOSE>
USD Index D 20150801 0 97.199 97.336 97.191 97.192
USD Index D 20150802 0 97.226 97.294 97.207 97.257
USD Index D 20150803 0 97.255 97.582 97.155 97.499


This is a basic question that needs to be at the top of search results. Thank you in advance for helping me to resolve this issue!

Answer

We can use read.table

setNames(read.table(text=dat[,1], sep=";", stringsAsFactors=FALSE), 
           scan(text=names(dat), sep=";", what = "", quiet = TRUE))
#    <TICKER> <PER>   <DATE> <TIME> <OPEN> <HIGH>  <LOW> <CLOSE>
# 1 USD Index     D 20150801      0 97.199 97.336 97.191  97.192
# 2 USD Index     D 20150802      0 97.226 97.294 97.207  97.257
# 3 USD Index     D 20150803      0 97.255 97.582 97.155  97.499

data

dat <- structure(list(`<TICKER>;<PER>;<DATE>;<TIME>;<OPEN>;<HIGH>;<LOW>;<CLOSE>` =
 c("USD Index;D;20150801;000000;97.199;97.336;97.191;97.192", 
"USD Index;D;20150802;000000;97.226;97.294;97.207;97.257", 
"USD Index;D;20150803;000000;97.255;97.582;97.155;97.499"
 )), .Names = "<TICKER>;<PER>;<DATE>;<TIME>;<OPEN>;<HIGH>;<LOW>;<CLOSE>", 
 class = "data.frame", row.names = c(NA, -3L))