[R] create new rows with 0, if nessecary
arun
smartpink111 at yahoo.com
Fri May 16 16:17:06 CEST 2014
Hi David,
It created duplicated Cat.
gg1
Cat Hours
1 101 0
2 101 15
3 102 0
4 103 0
5 103 10
6 104 0
7 105 0
8 106 0
#Probably,
gg1[!duplicated(gg1$Cat,fromLast=TRUE),]
Cat Hours
2 101 15
3 102 0
5 103 10
6 104 0
7 105 0
8 106 0
A.K.
On Friday, May 16, 2014 10:09 AM, David L Carlson <dcarlson at tamu.edu> wrote:
A bit simpler is to add the Hours column when creating the dd data.frame:
dd <- data.frame(Cat=min(aa[,1],bb[,1]):max(aa[,1],bb[,1]), Hours=0)
Then just
gg1 <- merge(aa,dd,all=TRUE)
gg2 <- merge(bb,dd,all=TRUE)
David Carlson
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Martyn Byng
Sent: Friday, May 16, 2014 6:51 AM
To: Mat; r-help at r-project.org
Subject: Re: [R] create new rows with 0, if nessecary
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:9}}
______________________________________________
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.
More information about the R-help
mailing list