Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Convert <key, val> pair into data.frame

In R, I have two vectors of pairs like this:

x <- c("A=5", "B=1",        "D=1", "E=1", "F=2", "G=1")
y <- c("A=2", "B=1", "C=3", "D=1",                     "H=4")

I would like to convert this into a data.frame like this:

  A B C D E F G H
x 5 1 0 1 1 2 1 0
y 2 1 3 1 0 0 0 4

All keys contained in either x or y should make up the columns, keys that do not appear in x or y should be added with value zero.

like image 444
Chris Avatar asked Nov 14 '11 20:11

Chris


1 Answers

Here's an environment based approach. Make separate environments into which the name=val pairs are evaluated. Then merge them:

xe <- new.env()
ye <- new.env()
with(xe, eval(parse(text=x)))
with(ye, eval(parse(text=y)))
# > ls(env=ye)
# [1] "A" "B" "C" "D" "H"
# edit as. list makes even more compact!
 df1 <- merge(as.list(xe), as.list(ye), all=TRUE, sort=FALSE)  
 # sort keeps row order with x on top!
  A B D  E  F  G  C  H
1 5 1 1  1  2  1 NA NA
2 2 1 1 NA NA NA  3  4

 df1[is.na(df1)] <- 0
 df1
  A B D E F G C H
1 2 1 1 0 0 0 3 4
2 5 1 1 1 2 1 0 0

The problem with the two arguments being equal leading to loss of one line is solved with the reshape::rbind.fill method.

df1 <- rbind.fill(as.data.frame(as.list(xe)), as.data.frame(as.list(ye)) )
like image 108
IRTFM Avatar answered Sep 28 '22 21:09

IRTFM