[R] How to replace zero with the character value- need help
arun
smartpink111 at yahoo.com
Wed Apr 10 16:10:32 CEST 2013
Hi,
"
>Thank You, Arun Kirshna , for your efforts and time.
>I also found an equivalent code:
>library(zoo)
>dat[] <- lapply(dat, function(x){replace(x, x == 0, NA)})
>dat <-na.locf(dat)
"
No problem.
If you have equal replications, you could also use:
dat<- structure(list(val = c(-1.11106874013409, 0.594715240107186,
0.0385480687548155, -0.00661093746677413, 0.464756104226324,
-0.205171655243946, -0.292331074714542, 0.773333870981288, 1.29174969807382,
0.362345750986263, -0.633848674579181, 0.424879532883622), state = c("TN",
"0", "0", "AP", "0", "0", "AL", "0", "0", "AZ", "0", "0"), country = c("India",
"0", "0", "0", "0", "0", "US", "0", "0", "0", "0", "0")), .Names = c("val",
"state", "country"), row.names = c(NA, -12L), class = "data.frame")
dat2<- dat
datNew<- dat[-3,] #to make unequal replications
dat[,-1]<-lapply(dat[,-1],function(x) x[rep(which(x!=0),each=length(x)/length(unique(x[x!=0])))])
dat
# val state country
#1 -1.111068740 TN India
#2 0.594715240 TN India
#3 0.038548069 TN India
#4 -0.006610937 AP India
#5 0.464756104 AP India
#6 -0.205171655 AP India
#7 -0.292331075 AL US
#8 0.773333871 AL US
#9 1.291749698 AL US
#10 0.362345751 AZ US
#11 -0.633848675 AZ US
#12 0.424879533 AZ US
#or
dat2[dat2==0]<-NA
dat2[,-1]<-lapply(dat2[,-1],function(x) x[rep(which(!is.na(x)),each=length(x)/length(unique(x[!is.na(x)])))])
identical(dat,dat2)
#[1] TRUE
#unequal reps
datNew[,-1]<-lapply(datNew[,-1],function(x) {x[rep(which(x!=0),diff(c(which(x!=0),length(x)+1)))]})
datNew
# val state country
#1 -1.111068740 TN India
#2 0.594715240 TN India
#4 -0.006610937 AP India
#5 0.464756104 AP India
#6 -0.205171655 AP India
#7 -0.292331075 AL US
#8 0.773333871 AL US
#9 1.291749698 AL US
#10 0.362345751 AZ US
#11 -0.633848675 AZ US
#12 0.424879533 AZ US
A.K.
----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: R help <r-help at r-project.org>
Cc:
Sent: Monday, April 8, 2013 9:10 AM
Subject: How to replace zero with the character value- need help
Hi,
Not sure if you have only one "country" or not.
Try this:
dat<- data.frame(val,state,country,stringsAsFactors=FALSE)
dat$country[dat$country==0]<-dat$country[1]
#or
#dat$country[dat$country==0]<- dat$country[dat$country!=0]
res<-do.call(rbind,lapply(split(dat,cumsum(grepl("[A-Za-z]",dat$state))),function(x) {x$state[x$state==0]<- x$state[1];x}))
#or
#res<- do.call(rbind,lapply(split(dat,cumsum(grepl("[A-Za-z]",dat$state))),function(x) {x$state[x$state==0]<- x$state[x$state!=0];x}))
row.names(res)<- 1:nrow(res)
res
# val state country
#1 1.50643668 TN India
#2 -0.88024059 TN India
#3 0.35025608 TN India
#4 -0.08874850 AP India
#5 -1.69222182 AP India
#6 0.09479274 AP India
A.K.
Respected Sir/Madam
The dataset I have, given below.
set.seed <- (1)
val <- rnorm(6)
state <- c("TN",0,0,"AP",0,0)
country <- c("India",0,0,0,0,0)
dat <- as.data.frame(cbind(val,state,country))
The dataset I need is given
state1 <- c("TN","TN","TN","AP","AP","AP")
country1 <- c("India","India","India","India","India","India")
dat1 <- as.data.frame(cbind(val,state1,country1))
Please help me or direct me to fill the zeros with the appropriate character values.
Thanking you in advance
More information about the R-help
mailing list