0

I am trying to create such list with value from different data frame called kc2 to kc10. anyone provide me some advice how to formulate this for loop?

sum_square=append(sum_square,weighted.mean(x=kc2$withinss,w=kc2$size, na.rm=TRUE))

I tried something like this but didnt work:

for (i in 2:10){
   nam1 = paste0("kc",i,"$withinss")
   nam2 = paste0("kc",i,"$size")
   sum_square = append(sum_square, lapply(c(as.numeric(nam1),as.numeric(nam2)), weighted.mean))
 }
smci
  • 32,567
  • 20
  • 113
  • 146
peteraa
  • 11
  • 1
  • 1
    Put the code back as plain text, not as an image... – Aaron left Stack Overflow Apr 23 '18 at 21:21
  • For formatting help, see https://stackoverflow.com/help/formatting – Aaron left Stack Overflow Apr 23 '18 at 21:23
  • 3
    peteraa, realize that most of us are volunteering time to help; please make it easy for us to help by making examples as copyable and reproducible as feasible. Your goal is to make it easy to understand (aesthetics, clarity of your question/needs/expectations) and easy to reproduce (don't ask us to transcribe data which is just as easily copied by you into the question textbox). If nothing else, if you don't spend much time on the question, why should I spend *any* time on an answer? – r2evans Apr 23 '18 at 21:26
  • sry guys still a rookie here. will do better – peteraa Apr 23 '18 at 21:28
  • peteraa, it would be helpful to have some representative data. Please read about [minimal working examples](https://stackoverflow.com/help/mcve) and a good SO q&a about [reproducible examples](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), for instance adding the output from `dput(head(kc2))` might be useful here. But really, please read the links, it will help you help us in the long run. – r2evans Apr 23 '18 at 21:40
  • If you just want to programmatically access dataframe names from `kc2..kc10`, then use the [`assign()`](https://stat.ethz.ch/R-manual/R-devel/library/base/html/assign.html) statement, and see existing SO questions. But really if you want to gather multiple dataframes together and process them, use the [`tidyverse`](https://www.tidyverse.org/) packages, they will make your life sane. – smci Apr 23 '18 at 22:14
  • @r2evans "*most* of us are volunteering time to help". Does that mean that some of us are getting paid for this? If so, how do I sign up? – John Coleman Apr 24 '18 at 13:30
  • Hehe, that's more me being defensive ... I don't "know" that not one person is getting paid ... but I too would like that. (Though, to be fair, if any answer is written while "at work", it's kinda like being paid for it ... guiltily ... amirite?) – r2evans Apr 24 '18 at 14:16

1 Answers1

1

There are a lot of problems with the code you posted, so I'll just cut right to the point. In R, when you want to apply a function to multiple objects and collect the result, you should be thinking of using lapply. lapply loops through a list of objects (you can put your data frames into a list), applies the chosen function to each, and then returns the result of each as a list. The below code is in the form of what you want:

# Add data frames to list by name
list_of_data_frames <- list(kc2, kc3, kc4, kc5, kc6, kc7, kc8, kc9, kc10)
# OR add them programatically
list_of_data_frames <- mget(paste0('kc', seq.int(from = 2, to = 10)))

result <- lapply(list_of_data_frames,
                 function(x) weighted.mean(x = x$withiniss, w = x$size, na.rm=TRUE))
divibisan
  • 11,659
  • 11
  • 40
  • 58
  • thanks for the reply. however I still got this error: Error in x$withiniss : $ operator is invalid for atomic vectors – peteraa Apr 24 '18 at 05:55
  • 1
    Did you put your data frames into the list `list_of_data_frames`? – Aaron left Stack Overflow Apr 24 '18 at 12:51
  • As @Aaron said, you need to put your data frames into a list. If you give the above function a list, it will apply the function to each element of the list (each data frame in your case). If you give it a data.frame, though, it will apply it to each **variable/column** in that data.frame, which is why you get that error. – divibisan Apr 24 '18 at 13:24