I would like a function to strip quotes off character strings. I should work like this: > A <- matrix(1:6, nrow = 2, ncol=3) > AF <- as.data.frame(A) > names(AF) <- c("First","Second","Third") > AF First Second Third 1 1 3 5 2 2 4 6 > names(AF)[2] [1] "Second" > attach(AF) > unquote(names(AF)[2]) [1] 3 4 Of course what I actually get is Error: couldn't find function "unquote" The reason that I want to do this is that I have a frame with a rather large number of variables and I would like to loop over the names and print out various descriptive summaries of the variable's distribution. OK, OK, I could just go > AF[,2] [1] 3 4 but once I thought of unquoting I have some sort of inner need to know how to do it! Cheers, Murray -- Dr Murray Jorgensen http://www.stats.waikato.ac.nz/Staff/maj.html Department of Statistics, University of Waikato, Hamilton, New Zealand Email: maj at waikato.ac.nz Fax 7 838 4155 Phone +64 7 838 4773 wk Home +64 7 825 0441 Mobile 021 1395 862
Try these get(names(AF)[2]) AF["Second"] # this one different than the rest AF[["Second"]] AF[, "Second"] AF$Second On 8/20/06, Murray Jorgensen <maj at waikato.ac.nz> wrote:> I would like a function to strip quotes off character strings. I should > work like this: > > > A <- matrix(1:6, nrow = 2, ncol=3) > > AF <- as.data.frame(A) > > names(AF) <- c("First","Second","Third") > > AF > First Second Third > 1 1 3 5 > 2 2 4 6 > > names(AF)[2] > [1] "Second" > > attach(AF) > > unquote(names(AF)[2]) > [1] 3 4 > > Of course what I actually get is > > Error: couldn't find function "unquote" > > The reason that I want to do this is that I have a frame with a rather > large number of variables and I would like to loop over the names and > print out various descriptive summaries of the variable's distribution. > > OK, OK, I could just go > > > AF[,2] > [1] 3 4 > > but once I thought of unquoting I have some sort of inner need to know > how to do it!Try: sapply(names(AF), function(nm) mean(AF[nm])) # same as previous but names a bit nicer sapply(names(AF), function(nm) mean(AF[nm]), USE.NAMES = FALSE) sapply(AF, mean) colMeans(AF)> > Cheers, > > Murray > -- > Dr Murray Jorgensen http://www.stats.waikato.ac.nz/Staff/maj.html > Department of Statistics, University of Waikato, Hamilton, New Zealand > Email: maj at waikato.ac.nz Fax 7 838 4155 > Phone +64 7 838 4773 wk Home +64 7 825 0441 Mobile 021 1395 862 > > ______________________________________________ > 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. >
Murray Jorgensen <maj at waikato.ac.nz> writes:> I would like a function to strip quotes off character strings. I should > work like this: > > > A <- matrix(1:6, nrow = 2, ncol=3) > > AF <- as.data.frame(A) > > names(AF) <- c("First","Second","Third") > > AF > First Second Third > 1 1 3 5 > 2 2 4 6 > > names(AF)[2] > [1] "Second" > > attach(AF) > > unquote(names(AF)[2]) > [1] 3 4 > > Of course what I actually get is > > Error: couldn't find function "unquote" > > The reason that I want to do this is that I have a frame with a rather > large number of variables and I would like to loop over the names and > print out various descriptive summaries of the variable's distribution. > > OK, OK, I could just go > > > AF[,2] > [1] 3 4 > > but once I thought of unquoting I have some sort of inner need to know > how to do it!Anything wrong with AF[,names(AF)[2]] or AF[[names(AF)[2]]] or for that matter eval(as.name(names(AF)[2]), envir=AF) ?? -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
?get I really think this has nothing to do with `quoting', rather to do with evaluating variables from their names. At first I though you were looking for noquote(), which does unquote in the conventional sense.> noquote(names(AF)[2])[1] Second> get(names(AF)[2])[1] 3 4 On Sun, 20 Aug 2006, Murray Jorgensen wrote:> I would like a function to strip quotes off character strings. I should > work like this: > > > A <- matrix(1:6, nrow = 2, ncol=3) > > AF <- as.data.frame(A) > > names(AF) <- c("First","Second","Third") > > AF > First Second Third > 1 1 3 5 > 2 2 4 6 > > names(AF)[2] > [1] "Second" > > attach(AF) > > unquote(names(AF)[2]) > [1] 3 4 > > Of course what I actually get is > > Error: couldn't find function "unquote" > > The reason that I want to do this is that I have a frame with a rather > large number of variables and I would like to loop over the names and > print out various descriptive summaries of the variable's distribution. > > OK, OK, I could just go > > > AF[,2] > [1] 3 4 > > but once I thought of unquoting I have some sort of inner need to know > how to do it! > > Cheers, > > Murray >-- 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