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

Prof Brian Ripley ripley at stats.ox.ac.uk
Sun Dec 3 23:46:03 CET 2006


I think you want

scoring <- function(x, A){
    nm <- deparse(substitute(A))
    x[, nm] <- rowSums(x[, A])
    x
}


On Sun, 3 Dec 2006, Gary Collins wrote:

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

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list