[R] passing an argument to a function which is also to be a dataframe column name

Gary Collins collins.gs at gmail.com
Sun Dec 3 23:28:46 CET 2006


any suggestions on the following gratefully welcome,

I have a dataframe, which I am subsetting via labels

atpi[, creativity]

where (for example)

atpi = as.data.frame(matrix(1:50, ncol = 5, nrow = 10))
names(atpi) = c("Q1", "Q2", "Q3", "Q4", "Q5")

and

creativity = c("Q1", "Q3", "Q4")

I want to add an extra column to the dataframe atpi labelled "creativity",
which is the rowsum of these 3 columns.  My problem is not the rowsum but
but the passing of an argument to a function, which is being used for subsetting
but also for labelling the rowsum column. I can do a one-off hack for
creativity,
but I want to generalise this, as I have numerous subsets
like creativity) to calculate and some pre-processiing (imputing
missing data etc)...
so I'm looking for something simple along the lines of

scoring = function(x, A){
	x[, A] = rowSums(x[, A])
	x
}

where a call such as

scoring(atpi, creativity)

would produce something along the lines of

> atpi
   Q1 Q2 Q3 Q4 Q5 creativity
1   1 11 21 31 41         53
2   2 12 22 32 42         56
3   3 13 23 33 43         59
4   4 14 24 34 44         62
5   5 15 25 35 45         65
6   6 16 26 36 46         68
7   7 17 27 37 47         71
8   8 18 28 38 48         74
9   9 19 29 39 49         77
10 10 20 30 40 50         80

Thanks in advance

Gary




More information about the R-help mailing list