hie l would like to create a 6th column "actual surv time" from the following data the condition being if censoringTime>survivaltime then actual survtime =survival time else actual survtime =censoring time the code l used to create the data is s=2 while(s!=0){ n=20 m<-matrix(nrow=n,ncol=4) colnames(m)=c("treatmentgrp","strata","censoringTime","survivalTime") for(i in 1:20) m[i,]<-c(sample(c(1,2),1,replace=TRUE),sample(c(1,2),1,replace=TRUE),rexp(1,.007),rexp(1,.002)) m<-cbind(m,0) m[m[,3]>m[,4],5]<-1 colnames(m)[5]<-"censoring" print(m) s=s-1 treatmentgrp strata censoringTime survivalTime censoring [1,] 1 1 1.012159 1137.80922 0 [2,] 2 2 32.971439 247.21786 0 [3,] 2 1 85.758253 797.04949 0 [4,] 1 1 16.999171 78.92309 0 [5,] 2 1 272.909896 298.21483 0 [6,] 1 2 138.230629 935.96765 0 [7,] 2 2 91.529859 141.08405 0 l keep getting an error message when i try to create the 6th column --------------------------------- [[alternative HTML version deleted]]
To create 6th column in the matrix m, you should use the cbind function. To calculate the vector of pairwise min or max values, you should use the pmin and pmax functions: act.surv.time<-pmin(m[,"censoringTime"],m[,"survivalTime"]) m<-cbind(m,act.surv.time) raymond chiruka wrote:> > hie l would like to create a 6th column "actual surv time" from the > following data > > the condition being > if censoringTime>survivaltime then actual survtime =survival time > else actual survtime =censoring time > > the code l used to create the data is > > s=2 > while(s!=0){ n=20 > m<-matrix(nrow=n,ncol=4) > > colnames(m)=c("treatmentgrp","strata","censoringTime","survivalTime") > for(i in 1:20) > m[i,]<-c(sample(c(1,2),1,replace=TRUE),sample(c(1,2),1,replace=TRUE),rexp(1,.007),rexp(1,.002)) > m<-cbind(m,0) > m[m[,3]>m[,4],5]<-1 > colnames(m)[5]<-"censoring" > print(m) > s=s-1 > treatmentgrp strata censoringTime survivalTime censoring > [1,] 1 1 1.012159 1137.80922 0 > [2,] 2 2 32.971439 247.21786 0 > [3,] 2 1 85.758253 797.04949 0 > [4,] 1 1 16.999171 78.92309 0 > [5,] 2 1 272.909896 298.21483 0 > [6,] 1 2 138.230629 935.96765 0 > [7,] 2 2 91.529859 141.08405 0 > > > l keep getting an error message when i try to create the 6th column > > --------------------------------- > ______________________________________________ > R-help at stat.math.ethz.ch 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. >-- View this message in context: http://www.nabble.com/creating-a-new-column-tf3704043.html#a10358728 Sent from the R help mailing list archive at Nabble.com.
Hallo, I just now a solution for da data frame. I'm not sure if this is what you want. Just try if it helps. Here an example of my code where I added a column: df <- rbind( c("fred","mary",4),c("fred","mary",7), c("fred","mary",9),c("barney","liz",3), c("barney","liz",5)) df <- data.frame(df) colnames(df) <- c("father","mother","child.age") # adding column df <- data.frame(df,"weddingdate"=c("Dec 12th, 1980","Dec 12th, 1980", "Dec 12th, 1980","Apr 9th, 2003", "Apr 9th, 2003")) df The R-Gui Result: father mother child.age weddingdate 1 fred mary 4 Dec 12th, 1980 2 fred mary 7 Dec 12th, 1980 3 fred mary 9 Dec 12th, 1980 4 barney liz 3 Apr 9th, 2003 5 barney liz 5 Apr 9th, 2003 Caution: the number of entries in adding column must correspond to the number of rows in your existing data frame df (here 5) Try this soultion, Corinna -----Urspr?ngliche Nachricht----- Von: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] Im Auftrag von raymond chiruka Gesendet: Montag, 7. Mai 2007 16:28 An: r Betreff: [R] creating a new column hie l would like to create a 6th column "actual surv time" from the following data the condition being if censoringTime>survivaltime then actual survtime =survival time else actual survtime =censoring time the code l used to create the data is s=2 while(s!=0){ n=20 m<-matrix(nrow=n,ncol=4) colnames(m)=c("treatmentgrp","strata","censoringTime","survivalTime") for(i in 1:20) m[i,]<-c(sample(c(1,2),1,replace=TRUE),sample(c(1,2),1,replace=TRUE),rexp(1,.007),rexp(1,.002)) m<-cbind(m,0) m[m[,3]>m[,4],5]<-1 colnames(m)[5]<-"censoring" print(m) s=s-1 treatmentgrp strata censoringTime survivalTime censoring [1,] 1 1 1.012159 1137.80922 0 [2,] 2 2 32.971439 247.21786 0 [3,] 2 1 85.758253 797.04949 0 [4,] 1 1 16.999171 78.92309 0 [5,] 2 1 272.909896 298.21483 0 [6,] 1 2 138.230629 935.96765 0 [7,] 2 2 91.529859 141.08405 0 l keep getting an error message when i try to create the 6th column --------------------------------- [[alternative HTML version deleted]] ______________________________________________ R-help at stat.math.ethz.ch 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.
Hallo, if you work with matrix you can use cbind to add columns. Corinna -----Urspr?ngliche Nachricht----- Von: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] Im Auftrag von raymond chiruka Gesendet: Montag, 7. Mai 2007 16:28 An: r Betreff: [R] creating a new column hie l would like to create a 6th column "actual surv time" from the following data the condition being if censoringTime>survivaltime then actual survtime =survival time else actual survtime =censoring time the code l used to create the data is s=2 while(s!=0){ n=20 m<-matrix(nrow=n,ncol=4) colnames(m)=c("treatmentgrp","strata","censoringTime","survivalTime") for(i in 1:20) m[i,]<-c(sample(c(1,2),1,replace=TRUE),sample(c(1,2),1,replace=TRUE),rexp(1,.007),rexp(1,.002)) m<-cbind(m,0) m[m[,3]>m[,4],5]<-1 colnames(m)[5]<-"censoring" print(m) s=s-1 treatmentgrp strata censoringTime survivalTime censoring [1,] 1 1 1.012159 1137.80922 0 [2,] 2 2 32.971439 247.21786 0 [3,] 2 1 85.758253 797.04949 0 [4,] 1 1 16.999171 78.92309 0 [5,] 2 1 272.909896 298.21483 0 [6,] 1 2 138.230629 935.96765 0 [7,] 2 2 91.529859 141.08405 0 l keep getting an error message when i try to create the 6th column --------------------------------- [[alternative HTML version deleted]] ______________________________________________ R-help at stat.math.ethz.ch 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 without knowing your code, R version and error message it is hard to say what is wrong. I think I answered already this or similar question but nevertheless: If your data are in data frame ifelse(mm$censoringTime>mm$survivalTime,mm$survivalTime, mm$censoringTime) gives you a vector of required values if you have matrix ifelse(m[,3]>m[,4],m[,4], m[,3]) gives you the same. Sou you need to add it to your existing structure by cbind() or data.frame() Regards Petr petr.pikal at precheza.cz r-help-bounces at stat.math.ethz.ch napsal dne 07.05.2007 16:27:37:> hie l would like to create a 6th column "actual surv time" from thefollowing data> > the condition being > if censoringTime>survivaltime then actual survtime =survival time > else actual survtime =censoring time > > the code l used to create the data is > > s=2 > while(s!=0){ n=20 > m<-matrix(nrow=n,ncol=4) > colnames(m)=c("treatmentgrp","strata","censoringTime","survivalTime") > for(i in 1:20)m[i,]<-c(sample(c(1,2),1,replace=TRUE),sample(c(1,2),> 1,replace=TRUE),rexp(1,.007),rexp(1,.002)) > m<-cbind(m,0) > m[m[,3]>m[,4],5]<-1 > colnames(m)[5]<-"censoring" > print(m) > s=s-1 > treatmentgrp strata censoringTime survivalTime censoring > [1,] 1 1 1.012159 1137.80922 0 > [2,] 2 2 32.971439 247.21786 0 > [3,] 2 1 85.758253 797.04949 0 > [4,] 1 1 16.999171 78.92309 0 > [5,] 2 1 272.909896 298.21483 0 > [6,] 1 2 138.230629 935.96765 0 > [7,] 2 2 91.529859 141.08405 0 > > > l keep getting an error message when i try to create the 6th column > > > > > > --------------------------------- > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.