function - Repeat n times a subset and plot in R -
first of forum because i've finded lot of answers!! time ask help. can solve this.... function?, loop?... didn't find example
# data.frame = data # a:f name of columns x<-unique(data$a) # in example c('var1','var2','var3','var4') y<-unique(data$b) # in example c('vara','varb') z<-unique(data$c) # in example c('var1a','var2a') # need repeat (based on x_y_z combinations)# x_y_z<-subset(data,data$a==x & data$b==y & data$c==z) plot_x_y<-qic(y=d ,n=e ,x=f ,data=x_y_z ,chart = 'p')
the idea repeat subsetting , make plot each x_y_z combination. subset should have name of variables combinated separated '_'. guess should work this:
var1_vara_var1a<-subset(data,data$a==var1 & data$b==vara & data$c==var1a) plot_var1_vara<-qic(y=d ,n=e ,x=f ,data=var1_vara_var1a ,chart = 'p')
and obtain plots:
plot_var1_vara_var1a plot_var1_varb_var1a plot_var1_vara_var2a plot_var1_varb_var2a plot_var2_vara_var1a plot_var2_varb_var1a plot_var2_vara_var2a plot_var2_varb_var2a plot_var3_vara_var1a plot_var3_varb_var1a plot_var3_vara_var2a plot_var3_varb_var2a plot_var4_vara_var1a plot_var4_varb_var1a plot_var4_vara_var2a plot_var4_varb_var2a
sorry basic question, i'm stuck on this.
cristobal
consider by
slices subset
allows dataframe operations in fun
arg. , use list of plots instead of many separately named plot objects.
plot_list <- by(data, data[, c("a","b","c")], fun = function(df) { qic(y = d,n = e,x = f, data = df, chart = 'p') })
should want rename list:
dfnames <- expand.grid(x,y,z) listnames <- vapply(1:nrow(dfnames), function(i) paste(dfnames$var1[[i]], dfnames$var2[[i]], dfnames$var3[[i]], sep="_"), character(1)) # [1] "var1_vara_var1a" "var2_vara_var1a" "var3_vara_var1a" "var4_vara_var1a" "var1_varb_var1a" # [6] "var2_varb_var1a" "var3_varb_var1a" "var4_varb_var1a" "var1_vara_var2a" "var2_vara_var2a" # [11] "var3_vara_var2a" "var4_vara_var2a" "var1_varb_var2a" "var2_varb_var2a" "var3_varb_var2a" # [16] "var4_varb_var2a" # rename list elements plot_list <- setnames(plot_list, paste0("plot_", listnames)) plot_list$plot_var1_vara_var1a # first plot plot_list$plot_var2_vara_var1a # second plot ...
Comments
Post a Comment