Re: [R] creating a new column
I haven't been following your discussion, but based on your method, the following should also work and would be the one-line equivalent: m$act.surv.time<-pmin(m[,"censoringTime"],m[,"survivalTime"]) In other words, you can add a column by just assigning a value to a new column name. You can also use the following syntax: m$act.surv.time<-pmin(m$"censoringTime",m$"survivalTime") I just thought you might want to see different version of accomplishing the same thing. Roger. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of raymond chiruka Sent: Tuesday, May 08, 2007 12:36 PM To: Schmitt, Corinna; r Subject: Re: [R] creating a new column thanks for the help But l ened up using act.surv.time<-pmin(m[,"censoringTime"],m[,"survivalTime"]) m<-cbind(m,act.surv.time) which seems to work thanks "Schmitt, Corinna" <[EMAIL PROTECTED]> wrote: Hallo, I just tried, if my solution might be possible for you. Here is the result: > 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 + } #bilding a data frame from m x=data.frame(m) # adding a column, where nrow(x) = number of row in x and in the # new coulmn should stand the entries 1...20. x=data.frame(x,"actual surv time"=c(1:nrow(x))) > x treatmentgrp strata censoringTime survivalTime censoring actual.surv.time 1 1 2377.486125 1070.66287 01 2 1 2242.468604 1061.30474 02 3 1 2 40.904656 51.88263 03 4 2 2 44.025595590.15317 04 5 1 1253.093279247.32141 15 6 2 2 50.486272257.25016 06 7 1 1337.591250554.05931 07 8 2 2 74.905075873.14563 08 9 1 2 57.196581765.43142 09 101 2370.147307 1646.65368 0 10 111 2152.738532480.12355 0 11 122 2 15.313303139.19791 0 12 131 2 17.205624641.15764 0 13 142 1 81.753924107.02202 0 14 151 2 60.774221665.27500 0 15 162 1 8.712562142.90775 0 16 171 1 54.542722 1904.88060 0 17 182 2 85.626140214.66811 0 18 192 1 31.257923739.96591 0 19 201 1 85.910141306.14860 0 20 See it works! For more information of data frames, see ?data.frame Here are some additional examples. > x[2,6] # Extraction of special entries: x[2,6]= 2 -> row 2, column 6 [1] 2 > x[2,6]=100 # Changing entires: x[2,6]=100 > x treatmentgrp strata censoringTime survivalTime censoring actual.surv.time 1 1 2377.486125 1070.66287 01 2 1 2242.468604 1061.30474 0 100 3 1 2 40.904656 51.88263 03 4 2 2 44.025595590.15317 04 5 1 1253.093279247.32141 15 6 2 2 50.486272257.25016 06 7 1 1337.591250554.05931 07 8 2 2 74.905075873.14563 08 9 1 2 57.196581765.43142 09 101 2370.147307 1646.65368 0 10 111 2152.738532480.12355 0 11 122 2 15.313303139.19791 0 12 131 2 17.205624641.15764 0 13 142 1 81.753924107.02202 0 14 151 2 60.774221665.27500 0 15 162 1 8.712562142.90775 0 16 171 1 54.542722
Re: [R] creating a new column
50.486272257.25016 06 7 1 1337.591250554.05931 07 8 2 2 74.905075873.14563 08 9 1 2 57.196581765.43142 09 101 2370.147307 1646.65368 0 10 111 2152.738532480.12355 0 11 122 2 15.313303139.19791 0 12 131 2 17.205624641.15764 0 13 142 1 81.753924107.02202 0 14 151 2 60.774221665.27500 0 15 162 1 8.712562142.90775 0 16 171 1 54.542722 1904.88060 0 17 182 2 85.626140214.66811 0 18 192 1 31.257923739.96591 0 19 201 1 85.910141306.14860 0 20 > column3=t[,-3] # deleting a column > column3 treatmentgrp strata survivalTime censoring actual.surv.time 1 1 2 1070.66287 01 2 1 2 1061.30474 0 100 3 1 2 51.88263 03 4 2 2590.15317 04 5 1 1247.32141 15 6 2 2257.25016 06 7 1 1554.05931 07 8 2 2873.14563 08 9 1 2765.43142 09 101 2 1646.65368 0 10 111 2480.12355 0 11 122 2139.19791 0 12 131 2641.15764 0 13 142 1107.02202 0 14 151 2665.27500 0 15 162 1142.90775 0 16 171 1 1904.88060 0 17 182 2214.66811 0 18 192 1739.96591 0 19 201 1306.14860 0 20 Happy working, Corinna -Ursprüngliche Nachricht- Von: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Im Auftrag von Schmitt, Corinna Gesendet: Dienstag, 8. Mai 2007 10:06 An: raymond chiruka; r Betreff: Re: [R] creating a new column 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.ageweddingdate 1 fred mary 4 Dec 12th, 1980 2 fred mary 7 Dec 12th, 1980 3 fred mary 9 Dec 12th, 1980 4 barneyliz 3 Apr 9th, 2003 5 barneyliz 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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 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
Re: [R] creating a new column
Hallo, if you work with matrix you can use cbind to add columns. Corinna -Ursprüngliche Nachricht- Von: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 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.0121591137.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@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. __ R-help@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.
Re: [R] creating a new column
9 101 2370.147307 1646.65368 0 10 111 2152.738532480.12355 0 11 122 2 15.313303139.19791 0 12 131 2 17.205624641.15764 0 13 142 1 81.753924107.02202 0 14 151 2 60.774221665.27500 0 15 162 1 8.712562142.90775 0 16 171 1 54.542722 1904.88060 0 17 182 2 85.626140214.66811 0 18 192 1 31.257923739.96591 0 19 201 1 85.910141306.14860 0 20 > column3=t[,-3] # deleting a column > column3 treatmentgrp strata survivalTime censoring actual.surv.time 1 1 2 1070.66287 01 2 1 2 1061.30474 0 100 3 1 2 51.88263 03 4 2 2590.15317 04 5 1 1247.32141 15 6 2 2257.25016 06 7 1 1554.05931 07 8 2 2873.14563 08 9 1 2765.43142 09 101 2 1646.65368 0 10 111 2480.12355 0 11 122 2139.19791 0 12 131 2641.15764 0 13 142 1107.02202 0 14 151 2665.27500 0 15 162 1142.90775 0 16 171 1 1904.88060 0 17 182 2214.66811 0 18 192 1739.96591 0 19 201 1306.14860 0 20 Happy working, Corinna -Ursprüngliche Nachricht- Von: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Im Auftrag von Schmitt, Corinna Gesendet: Dienstag, 8. Mai 2007 10:06 An: raymond chiruka; r Betreff: Re: [R] creating a new column 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.ageweddingdate 1 fred mary 4 Dec 12th, 1980 2 fred mary 7 Dec 12th, 1980 3 fred mary 9 Dec 12th, 1980 4 barneyliz 3 Apr 9th, 2003 5 barneyliz 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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 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.0121591137.80922 0 [2,] 2 2 32.971439 247.21786 0 [3,] 2 1 85.758253 797.04949 0 [4,] 1 1 16.999171 78.
Re: [R] creating a new column
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.ageweddingdate 1 fred mary 4 Dec 12th, 1980 2 fred mary 7 Dec 12th, 1980 3 fred mary 9 Dec 12th, 1980 4 barneyliz 3 Apr 9th, 2003 5 barneyliz 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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 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.0121591137.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@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. __ R-help@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.
Re: [R] creating a new column
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.0121591137.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@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. __ R-help@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.
[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.0121591137.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@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.