# [R] creating a new column

Bos, Roger roger.bos at us.rothschild.com
Tue May 8 21:02:19 CEST 2007

```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: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] 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" <Corinna.Schmitt at igb.fraunhofer.de> 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      2    377.486125   1070.66287         0                1
2             1      2    242.468604   1061.30474         0                2
3             1      2     40.904656     51.88263         0                3
4             2      2     44.025595    590.15317         0                4
5             1      1    253.093279    247.32141         1                5
6             2      2     50.486272    257.25016         0                6
7             1      1    337.591250    554.05931         0                7
8             2      2     74.905075    873.14563         0                8
9             1      2     57.196581    765.43142         0                9
10            1      2    370.147307   1646.65368         0               10
11            1      2    152.738532    480.12355         0               11
12            2      2     15.313303    139.19791         0               12
13            1      2     17.205624    641.15764         0               13
14            2      1     81.753924    107.02202         0               14
15            1      2     60.774221    665.27500         0               15
16            2      1      8.712562    142.90775         0               16
17            1      1     54.542722   1904.88060         0               17
18            2      2     85.626140    214.66811         0               18
19            2      1     31.257923    739.96591         0               19
20            1      1     85.910141    306.14860         0               20

> 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      2    377.486125   1070.66287         0                1
2             1      2    242.468604   1061.30474         0              100
3             1      2     40.904656     51.88263         0                3
4             2      2     44.025595    590.15317         0                4
5             1      1    253.093279    247.32141         1                5
6             2      2     50.486272    257.25016         0                6
7             1      1    337.591250    554.05931         0                7
8             2      2     74.905075    873.14563         0                8
9             1      2     57.196581    765.43142         0                9
10            1      2    370.147307   1646.65368         0               10
11            1      2    152.738532    480.12355         0               11
12            2      2     15.313303    139.19791         0               12
13            1      2     17.205624    641.15764         0               13
14            2      1     81.753924    107.02202         0               14
15            1      2     60.774221    665.27500         0               15
16            2      1      8.712562    142.90775         0               16
17            1      1     54.542722   1904.88060         0               17
18            2      2     85.626140    214.66811         0               18
19            2      1     31.257923    739.96591         0               19
20            1      1     85.910141    306.14860         0               20
> t=x
> row2=t[-2,] # deleting a row
> row2
treatmentgrp strata censoringTime survivalTime censoring actual.surv.time
1             1      2    377.486125   1070.66287         0                1
3             1      2     40.904656     51.88263         0                3
4             2      2     44.025595    590.15317         0                4
5             1      1    253.093279    247.32141         1                5
6             2      2     50.486272    257.25016         0                6
7             1      1    337.591250    554.05931         0                7
8             2      2     74.905075    873.14563         0                8
9             1      2     57.196581    765.43142         0                9
10            1      2    370.147307   1646.65368         0               10
11            1      2    152.738532    480.12355         0               11
12            2      2     15.313303    139.19791         0               12
13            1      2     17.205624    641.15764         0               13
14            2      1     81.753924    107.02202         0               14
15            1      2     60.774221    665.27500         0               15
16            2      1      8.712562    142.90775         0               16
17            1      1     54.542722   1904.88060         0               17
18            2      2     85.626140    214.66811         0               18
19            2      1     31.257923    739.96591         0               19
20            1      1     85.910141    306.14860         0               20
> column3=t[,-3] # deleting a column
> column3
treatmentgrp strata survivalTime censoring actual.surv.time
1             1      2   1070.66287         0                1
2             1      2   1061.30474         0              100
3             1      2     51.88263         0                3
4             2      2    590.15317         0                4
5             1      1    247.32141         1                5
6             2      2    257.25016         0                6
7             1      1    554.05931         0                7
8             2      2    873.14563         0                8
9             1      2    765.43142         0                9
10            1      2   1646.65368         0               10
11            1      2    480.12355         0               11
12            2      2    139.19791         0               12
13            1      2    641.15764         0               13
14            2      1    107.02202         0               14
15            1      2    665.27500         0               15
16            2      1    142.90775         0               16
17            1      1   1904.88060         0               17
18            2      2    214.66811         0               18
19            2      1    739.96591         0               19
20            1      1    306.14860         0               20

Happy working,
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 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")

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
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
and provide commented, minimal, self-contained, reproducible code.

---------------------------------

[[alternative HTML version deleted]]

********************************************************************** *
This message is for the named person's use only. It may
contain confidential, proprietary or legally privileged
information. No right to confidential or privileged treatment
of this message is waived or lost by any error in
transmission. If you have received this message in error,
please immediately notify the sender by e-mail,
delete the message and all copies from your system and destroy
any hard copies. You must not, directly or indirectly, use,
disclose, distribute, print or copy any part of this message
if you are not the intended recipient.

```