Re: [R] creating a new column

From: Schmitt, Corinna <Corinna.Schmitt_at_igb.fraunhofer.de>
Date: Tue, 08 May 2007 11:31:24 +0200

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


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      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")

# 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.

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.

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. Received on Tue 08 May 2007 - 09:35:16 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 08 May 2007 - 17:31:06 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.