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
PIKAL Petr
2013-Apr-08 13:34 UTC
[R] How to replace zero with the character value- need help
Hi another option is to use na.locf from zoo, based on assumption that each zero has to be replaced with previous nonzero value. dat[dat==0]<-NA library(zoo) dat$state<-na.locf(dat$state) dat$country<-na.locf(dat$country)> datval state country 1 -0.543116777672352 TN India 2 -0.178662085411817 TN India 3 -2.30107974754641 TN India 4 0.24385819927209 AP India 5 -1.42584380820816 AP India 6 -1.17248639661816 AP India Petr> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of arun > Sent: Monday, April 08, 2013 3:10 PM > To: R help > Subject: [R] 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 > > > ______________________________________________ > 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.
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
I have a matrix of p values below. Column1 are the analytes and the other columns are p values comparing time points. How do I convert this table of pvalues into adjust p values for FDR/BH This is wrong does anyone know how to fix this code a<-read.table("file", header=T, sep"\t") b<-as.numeric(as.matrix((a[,2:8]))) p.adjust(b, "BH") Analyte? -0.5-8??????? -0.5-12?????? -0.5-24?????? -0.5-168????? 8-12? 12-24 24-168 1_1???? 0.98??? 0.31??? 0.71??? 0.73??? 0.54??? 0.08??? 0.02 2_1???? 0.51??? 0.03??? 0.65??? 0.64??? 0.18??? 0.02??? 0.90 3_1???? 0.92??? 0.18??? 0.49??? 0.93??? 0.12??? 0.04??? 0.37 4_1???? 0.18??? 0.52??? 0.86??? 0.17??? 0.68??? 0.68??? 0.67 5_1???? 0.20??? 0.59??? 0.05??? 0.61??? 0.24??? 0.02??? 0.17 6_1???? 0.86??? 0.92??? 0.83??? 0.23??? 0.74??? 0.92??? 0.97 7_1???? 0.86??? 0.28??? 0.04??? 0.58??? 0.20??? 0.00??? 0.08 8?????? 0.00??? 0.05??? 0.10??? 0.90??? 0.98??? 0.95??? 0.21 9?????? 0.00??? 0.03??? 0.04??? 0.37??? 0.77??? 0.86??? 0.18 10????? 0.00??? 0.07??? 0.10??? 0.90??? 1.00??? 0.98??? 0.18 11????? 0.05??? 0.04??? 0.14??? 0.14??? 0.74??? 0.86??? 0.87 12????? 0.00??? 0.31??? 0.07??? 0.09??? 0.02??? 0.28??? 0.00 13????? 0.80??? 0.42??? 0.98??? 0.09??? 0.21??? 0.42??? 0.04