[R] Iteration over variables
Rolf Turner
r.turner at auckland.ac.nz
Tue Sep 6 12:27:47 CEST 2016
You need to *print* results explicitly within a for-loop.
E.g.
print(head(d_dataset[,
c(paste0("v_turnover_",year),
paste0("v_customer_", year))]))
See FAQ 7.16 for a related discussion.
cheers,
Rolf Turner
--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276
On 06/09/16 11:54, G.Maubach at weinwolf.de wrote:
> Hi All,
>
> I would like to write a program that iterates over a set of dynamically
> generated variables and produces some stats or prints parts of the data.
>
> # --- data
> v_turnover_2011 <- c(10, 20, 30, 40 , 50)
> v_customer_2011 <- c(0, 1, NA, 0, 1)
> v_turnover_2012 <- c(10, 20, 30, 40 , 50)
> v_customer_2012 <- c(0, 1, NA, 0, 1)
> d_dataset <- data.frame(v_turnover_2011, v_turnover_2012,
> v_customer_2011, v_customer_2012)
>
> # -- Aim is to iterate over dynamically generated variables and compute
> # -- statistics or print parts of the data
>
> # -- Does not produce any output
> for (year in 2011:2012) {
> head(d_dataset[, c(paste0("v_turnover_", year),
> paste0("v_customer_", year))])
> }
>
> # -- Does not produce any output
> aux_func <- function(year) {
> head(d_dataset[, c(paste0("v_turnover_", year),
> paste0("v_customer_", year))])
> }
>
> for (year in 2011:2012) {
> aux_func(year = year)
> }
>
>
> d_results <- data.frame()
> for (year in 2011:2012) {
> d_results <- rbind(d_results,
> paste0("mean", year) = mean(d_dataset[,
> c(paste0("v_turnover_", year))]))
> }
>
> Is there a way to iterate over variables and compute statistics and print
> parts of the dataset?
More information about the R-help
mailing list