[R] insert value according to indice

Adrian Katschke adrian at atstatconsulting.com
Wed Feb 8 18:00:52 CET 2006


  R-Helpers,
   
  I am trying to insert a value into a dataframe. This value is a proportion calculated by counting the number of those individuals with that value and then inserting the proportion at the end of the dataframe to only those individuals with the given value. The problem I am running into is that the proportions are not being attached to only those individuals with the specified value for that proportion. 
   
  Below is an example of the code that I am using. I have also attached the data file that I am using to create this dataframe.
   
    #Read in Data
  age.int <- read.csv('C:/intage.csv',header = TRUE)
   

    asubs112 <- subset(age.int, rs1042364 != "(2,2)")

   
    ages112 <- sort(unique(na.omit(asubs112$first_drink)))
   
  for ( i in ages112) {
    indce <- which(na.omit(asubs112$first_drink == i))
    prop <- length(indce)/nrow(asubs112)
    asubs112[indce,5] <- prop
    asubs112[indce,]
  }
   
  Below is the output that I get from the script above. Notice the proportion for the first NA but not any of the others. Not sure what I am doing wrong, any suggestions are a big help.
   
  TIA,
  Adrian
   
   asubs112[1:50,]
      IND_ID rs1042364 first_drink age_int          V5
4   10008007     (1,2)          NA      16 0.003891051
6   10013012     (1,2)          13      14 0.116731518
7   10015006     (1,2)          12      17 0.105058366
8   10015007     (1,1)          12      16 0.105058366
10  10021009     (1,2)          NA      15          NA
14  10039036     (1,2)          NA      15          NA
15  10039037     (1,2)          NA      13          NA
17  10045005     (1,2)          13      17 0.116731518
18  10045014     (1,2)          13      14 0.116731518
21  10055022     (1,2)          NA      15          NA
22  10059006     (1,2)          12      NA 0.105058366
23  10060021     (1,2)          NA      15          NA
24  10060022     (1,2)          NA      13          NA
27  10069028     (1,2)          16      17 0.066147860
29  10071022     (1,1)          14      16 0.116731518
30  10071026     (1,2)          NA      13          NA
31  10071028     (1,2)          NA      13          NA
32  10071032     (1,2)          16      16 0.066147860
34  10072004     (1,2)          16      17 0.066147860
37  10073021     (1,2)          12      NA 0.105058366
39  10077013     (1,2)          NA      14          NA
40  10077076     (1,2)          15      15 0.062256809
41  10084005     (1,2)          14      15 0.116731518
44  10088017     (1,2)          15      15 0.062256809
50  10093005     (1,2)          NA      NA          NA
51  10096003     (1,2)          17      NA 0.023346304
56  10108005     (1,2)          14      NA 0.116731518
57  10112001     (1,2)           6      16 0.003891051
58  10114003     (1,1)          13      17 0.116731518
59  10114004     (1,1)          11      NA 0.027237354
60  10114005     (1,2)          12      14 0.105058366
61  10114006     (1,1)          13      17 0.116731518
62  10114007     (1,2)          15      15 0.062256809
63  10115006     (1,2)          13      14 0.116731518
69  10118011     (1,2)          14      14 0.116731518
70  10118012     (1,2)          16      17 0.066147860
73  10123024     (1,2)          13      15 0.116731518
76  10130019     (1,2)          NA      13          NA
77  10130020     (1,2)          12      15 0.105058366
78  10130021     (1,2)          NA      14          NA
87  10144039     (1,2)          12      13 0.105058366
91  10155016     (1,2)          NA      13          NA
92  10156004     (1,2)          14      15 0.116731518
99  10170009     (1,2)          16      17 0.066147860
100 10171016     (1,2)          NA      15          NA
104 10171027     (1,2)          13      14 0.116731518
107 10175013     (1,2)           9      16 0.011673152
108 10181013     (1,2)          11      13 0.027237354
110 10181017     (1,1)          14      16 0.116731518
111 10181024     (1,1)          16      17 0.066147860






More information about the R-help mailing list