Easy Way to Convert character array to dataframe

1.1k Views Asked by At

I am using R with Hadoop streaming where at the reducer, the value is a character array where each element is a string contains a few columns terminated by certain character, char(2) 002 in this case.

Is there an easy way to split the string into three fields and build a data frame from it?

Here is what I have done but I just have the feeling that I over engineered it again.

inputarray <- c("20130806\00211\00291.55", "20130807\00211\00291.55", "20130808\00211\00291.55", 
  "20130809\00211\00291.55", "201308010\00211\00291.55", "201308011\00211\00291.55", 
  "201308012\00211\00291.55", "201308013\00211\00291.55", "201308014\00211\00291.55"
)

tmp <- lapply(inputarray, FUN=function(x) strsplit(x, rawToChar(as.raw(2))) )
tmp <- data.frame(matrix(unlist(tmp), ncol=3, byrow=TRUE))
names(tmp) <- c("date", "qtyavail", "price")
tmp

Thanks!

2

There are 2 best solutions below

0
On BEST ANSWER

You could use read.table. First I add an element for the names at the beginning of inputarray

x <- c("date\002qtyavail\002price", inputarray)
read.table(text = x, sep = rawToChar(as.raw(2)), header = TRUE)
#        date qtyavail price
# 1  20130806       11 91.55
# 2  20130807       11 91.55
# 3  20130808       11 91.55
# 4  20130809       11 91.55
# 5 201308010       11 91.55
# 6 201308011       11 91.55
# 7 201308012       11 91.55
# 8 201308013       11 91.55
# 9 201308014       11 91.55

Alternatively, you could also use cSplit from the splitstackshape package

library(splitstackshape)
dt <- cSplit(data.table(x = inputarray), "x", rawToChar(as.raw(2)))
setnames(dt, names(dt), c("date", "qtyavail", "price"))
dt
#         date qtyavail price
# 1:  20130806       11 91.55
# 2:  20130807       11 91.55
# 3:  20130808       11 91.55
# 4:  20130809       11 91.55
# 5: 201308010       11 91.55
# 6: 201308011       11 91.55
# 7: 201308012       11 91.55
# 8: 201308013       11 91.55
# 9: 201308014       11 91.55
0
On

Here's a another option to do it using tidyr

library(tidyr)

separate(as.data.frame(inputarray), inputarray, 
         into = c("date", "qtyavail", "price"), sep = rawToChar(as.raw(2)))

#       date qtyavail price
#1  20130806       11 91.55
#2  20130807       11 91.55
#3  20130808       11 91.55
#4  20130809       11 91.55
#5 201308010       11 91.55
#6 201308011       11 91.55
#7 201308012       11 91.55
#8 201308013       11 91.55
#9 201308014       11 91.55

Note: your "inputarray" is a character vector not an array in R.