Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
143 views
in Technique[技术] by (71.8m points)

r - Use paste command with dplyr

I am trying to use paste command in order to paste some part of string into command group_by from dyplyr. This how is look like code normally without paste.

library(dplyr)
   DATA1<-DATA%>%
dplyr::group_by(id_n,gross_income)%>%
dplyr::summarize(gross_i=sum(gross_i)

So now I want to use paste command, and I try with this line of code.

  query_type1<-"id_n,gross_income"

Next step is to implement this line of code in code above.

   DATA1<-DATA%>%
dplyr::group_by(query_type1)%>%
dplyr::summarize(gross_i=sum(gross_i)

But unfortunately this don't give me good result. So can anybody help me how to fix this line of code?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As others have already suggested have column names as vector instead of one comma-separated string.

query_type1<- c("id_n","gross_income")

You can then use across in group_by :

library(dplyr)

DATA%>%
  group_by(across(query_type1)) %>%
  summarize(gross_i=sum(gross_i)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...