[R] Enquiry regarding Apply

Bob Green bgreen at dyson.brisnet.org.au
Tue May 2 14:41:42 CEST 2006


I want to compute a new variable (newvar) based on the values of two other 
variables (t1freq, t2freq).The two variables (t1freq,t2freq) are contained 
in a dataframe  - study1dat <- read.csv("c:\\study1rb.csv",header=T) .

I gather this computation can be done using Apply and I have run the 
following example from the help menu

  ## Compute row and column sums for a matrix:
      x <- cbind(x1 = 3, x2 = c(4:1, 2:5))
      dimnames(x)[[1]] <- letters[1:8]
      apply(x, 2, mean, trim = .2)
      col.sums <- apply(x, 2, sum)
      row.sums <- apply(x, 1, sum)
      rbind(cbind(x, Rtot = row.sums), Ctot = c(col.sums, sum(col.sums)))

My questions are:

1. I don't understand what values I would need to enter for my 2 variables 
(data is included at end of this message) in the 'cbind' command
2. For the second row, what modification is required?
3. If I wanted to compute a mean, I believe the 'mean' is substituted for 
sum - if so, in the case where there is only one value, is it possible to 
obtain this as the mean rather than obtaining an NA?
4. How do I save 'newvar' into the original datafile?

Any assistance with this enquiry is appreciated,

Bob


  id  t1freq t2freq
1       4      6
2       3      3
3       7      5
4       0      2
5       1      4
6       7      7
7       7      7
8       7      4
9       7      7
10      5      4
11      3      6
12      1      3
13      7      7
14      7      7
15      1      3
16      7      7
17      2      1
18      7      7
19      7      6
20      3      4
21      7      7
22      7      6
23      3      0
24      7      6
25      7      6
26      5      6
27      7      7
28      7      7
29      5      7
30      7      2
31      5      6
32      5      7
33      7      7
34      7      7
35      7      7
36      7      7
37      6      5
38      4      4
39      4      6
40      7      7
41      3      3
42      4      4
43      7      7
44      7      7
45      7      1
46      2      2
47      4      2
48      1      1
49      4      4
50      0      0
51      7      7
52      7      2
53      4      7
54      0     NA
55      5      4
56      0      0
57      5      5
58      5      6
59      7      7
60      5      4
61      1      4
62      6      7
63      4      5
64      6      7
65      5      4
66      1      0
67      7      7
68      5      3
69      7      7
70      4      7
71      3      7
72      7      7
73      4      2
74      4      6
75      7      5
76      0      0
77      7      6
78      3      3
79      7      7
80      2      4
81      4      1
82      1      4
83      4      2
84      2      1
85      5      0
86      1      3
87      0      0
88      7      7
89      3      1
90      6      6
91      2      1
92      4      3




More information about the R-help mailing list