[R] create new rows with 0, if nessecary
Martyn Byng
martyn.byng at nag.co.uk
Fri May 16 13:50:36 CEST 2014
Hi,
Something like:
aa <-structure(list(Cat = c(101, 103), Hours = c(15, 10)), .Names = c("Cat","Hours"), row.names = c(NA, -2L), class = "data.frame")
bb <-structure(list(Cat = c(103, 106), Hours = c(16, 11)), .Names = c("Cat","Hours"), row.names = c(NA, -2L), class = "data.frame")
dd <- data.frame(Cat=min(aa[,1],bb[,1]):max(aa[,1],bb[,1]))
gg1 <- merge(aa,dd,all=TRUE)
gg1[is.na(gg1[,2]),2] <- 0
gg2 <- merge(bb,dd,all=TRUE)
gg2[is.na(gg2[,2]),2] <- 0
Might be what you are looking for.
Martyn
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Mat
Sent: 16 May 2014 12:18
To: r-help at r-project.org
Subject: [R] create new rows with 0, if nessecary
hello togehter,
i have a little problem, i have frames like this two:
Cat Hours
A 101 15
B 103 10
Cat Hours
A 103 16
B 106 11
I need to convert this 2 data.frames into the following equal structure:
Cat Hours
A 101
B 102
C 103
D 104
E 105
F 106
The 2 examples should be look this one:
Cat Hours
A 101 15
B 102 0
C 103 10
D 104 0
E 105 0
F 106 0
Cat Hours
A 101 0
B 102 0
C 103 16
D 104 0
E 105 0
F 106 11
--
View this message in context: http://r.789695.n4.nabble.com/create-new-rows-with-0-if-nessecary-tp4690699.html
Sent from the R help mailing list archive at Nabble.com.
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
________________________________________________________________________
This e-mail has been scanned for all viruses by Star.\ _...{{dropped:3}}
More information about the R-help
mailing list