To calculate the total value of columns using R and extract only columns with a total value of 100 or more for each column

Asked 1 years ago, Updated 1 years ago, 399 views

Could you tell me how to use R to calculate the total value of the column and extract only columns with a total value of 100 or more into a data frame?

df<-data.frame(id=1:3, score1=c(200,30,100), score2=c(0,30,10), score3=c(90,40,10), score4=c(200,220,300))

The data frame looks like the one above.Thank you for your cooperation

r tidyverse

2022-12-15 14:31

1 Answers

tidyverse.

suppressMessages (library(tidyverse))

df<-data.frame (id=1:3, score1=c(200,30,100), score2=c(0,30,10), score3=c(90,40,10), score4=c(200,220,300))

df2<-df%>%select(id, where (~sum(.[.!="id"], na.rm=T)>=100))
df2

#   id score1 score3 score4
# 1  1    200     90    200
# 2  2     30     40    220
# 3  3    100     10    300


2022-12-15 21:40

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.