-3

I have a list of 11 dataframe and I want to apply a function that uses rowsums to create another column of sums for each row based on the specific criteria of matching a string in each of the 11 dataframe.

I have tried to use select(contains()). I am not looking at using mutate if possible then please show the error in the code below.

myfiles3 <- lapply(myfiles2, function(x) {x$CHINA2 <- rowSums(x[select(x, contains("China"))], na.rm = T); x})

It gives the error as Error in[.default(x, select(x, contains("China"))) : invalid subscript type 'list'.

ambrish dhaka
  • 689
  • 7
  • 27

2 Answers2

1

Very hard to test without sample data set. Try:

library(tidyverse)
library(hablar)

myfiles2 %>% 
  map_df(~.x %>% mutate(china2 = row_sum_(contains("China"))))
davsjob
  • 1,882
  • 15
  • 10
0

I don't know why there is so much impatience to tick of any question as -1 without sparing some thought that there is a huge diversity among the community here. Finally, solved this as follows:

myfiles3 <- lapply(myfiles2, function(x) {x$CHINA2 <- rowSums(x[,grep("China", names(x))], na.rm = T); x})
ambrish dhaka
  • 689
  • 7
  • 27
  • 1
    I would guess you got a downvote because you haven't provided a reproducible example rather just thrown some code into the question which we can't run or test. If you'd read the [How to as a good question?](https://stackoverflow.com/help/how-to-ask) tutorial, you would probably know better. – David Arenburg Jun 16 '19 at 13:59
  • 3
    Is there a particular reason why you have no accepted answer in your 10+ recent questions I see. Did none of them help you? Asking from the community without giving anything back not a good karma IMO. – Ronak Shah Jun 16 '19 at 14:02
  • Here is something that I definitely appreciate, raising the debate. You are engaging a social scientist. And, if you can appreciate this fact then you must also know that the way I have approached R, Python is purely from a very fundamental level. None of my code is going to add to your knowledge. But, my utilization of this knowledge is an invitation for you to see a point in this. – ambrish dhaka Jun 16 '19 at 14:08
  • 2
    Your code is irrelevant. It is only relevant to show that you made an effort to solve the problem- but we could offer other alternatives. Instead, now you stuck with only your own solution- why? Because you haven't provided a reproducible example accommodated by the desired output- so we basically can't help, which in turn, makes your question here useless. And no, we don't need 3 255x239 dataframes. Instead, 2 10X3 dataframes would be enough as long as it reproduces the problem. If you would made an effort to create such data sets you would be half way to the solution. – David Arenburg Jun 16 '19 at 14:17
  • 1
    You could also look [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for specific R related examples on how to create a reproducible example. – David Arenburg Jun 16 '19 at 14:18
  • "And no, we don't need 3 255x239 dataframes. Instead, 2 10X3 dataframes ....." If you had gone through my statements patiently then you wouldn't have come to this. I said that out of 239 columns only three are containing word "China". So, how to determine number of columns for `dput`? I couldn't arrive, sorry. – ambrish dhaka Jun 16 '19 at 14:30