[R] binary conversion list to data.frame with plyr... AND NO LOOPS!
Justin Haynes
jtor14 at gmail.com
Sat Jul 9 00:39:13 CEST 2011
Happy weekend helpeRs!
As usual, I'm stumped by R...
My plan was to take an integer number, convert it to binary and wind
up with a data.frame where each column is either 1 or 0 so I can see
which bits are changing:
bb<-function(i) ifelse(i, paste(bb(i %/% 2), i %% 2, sep=""), "")
my.dat<-c(36,40,10,4)
my.binary.dat<-bb(my.dat)
my.list<-strsplit(my.binary.dat,'')
max.len<-max(ldply(my.list,length))
len<-length(my.list)
my.df<-data.frame(two=rep(0,len),four=rep(0,len),eight=rep(0,len),sixteen=rep(0,len),thirtytwo=rep(0,len),sixtyfour=rep(0,len))
for(i in 1:length(my.list)){
for(j in 1:length(my.list[[i]])){
my.df[i,max.len-length(my.list[[i]])+j]<-my.list[[i]][j]
}
}
But this isn't exactly feasable on a million+ rows where some binary
numbers are 20 digits... I know theres a way without loops I just
know it!
Ideally, I can do this to multiple columns of a data.frame and have
them named accordingly (V1.two,V1.four... V2.two,V2.four, etc.)
Thanks,
Justin
More information about the R-help
mailing list