Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy Way to Convert character array to dataframe

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!

like image 626
B.Mr.W. Avatar asked Dec 03 '25 09:12

B.Mr.W.


1 Answers

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
like image 115
Rich Scriven Avatar answered Dec 05 '25 01:12

Rich Scriven



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!