Mean of categorical variables from plyr package

0 votes
My categorical variable, risk, is divided into three groups: ADV, HHM, and POV.

I'm looking for the average of these three groups for four continuous variables: read.5, read.6, read.7, and read.8, which are reading scores from grades 5 to 8.

It's the,2:5 of my dataset and an old textbook example. I used the following code, which appears to be incorrect despite the fact that it is correct according to the textbook example:

ddply(.data = MPLS[,2:5],.variables =.(MPLS$risk), myrisk

na.rm = TRUE,.fun = mean)

Earlier today, I received an error message for a section of code that read:

mean(MPLS[,2:5], na.rm = TRUE) - mymeans

When I googled it, I discovered that the R software had changed, and I needed to find another to operate with.
Jun 17, 2022 in Data Analytics by Avinash
• 1,260 points
315 views

1 answer to this question.

0 votes
df<-data.frame(risk= rep(c("ADV","HHM","POV"),10),
                read.5= rnorm(30,30),
                read.4= rnorm(30,30),
                read.3= rnorm(30,30),
                read.2= rnorm(30,30))
> head(df)
#  risk   read.5   read.4   read.3   read.2
#1  ADV 30.78281 30.00721 29.80906 29.25936
#2  HHM 29.76175 29.63864 29.39256 29.40070
#3  POV 29.00964 30.48258 29.20662 28.77509
#4  ADV 29.60631 30.35032 32.00376 30.70374
#5  HHM 31.38653 30.28896 29.48756 30.32430
#6  POV 30.33102 30.40897 29.55796 30.10585

library(dplyr)

df %>% group_by(risk) %>% summarise_all(mean)

# A tibble: 3 x 5
#  risk  read.5 read.4 read.3 read.2
#  <fct>  <dbl>  <dbl>  <dbl>  <dbl>
1 ADV     30.3   30.2   30.2   30.4
2 HHM     29.7   30.5   29.8   29.9
3 POV     29.3   30.2   29.9   30.2



answered Jun 24, 2022 by Sohail
• 3,040 points

Related Questions In Data Analytics

0 votes
0 answers

https://www.edureka.co/community/183864/mean-of-categorical-variables-from-plyr-package

I'm modelling with the fpp2 package's data ...READ MORE

Jun 17, 2022 in Data Analytics by Avinash
• 1,260 points
164 views
0 votes
1 answer

How to create dummy variables based on a categorical variable of lists in R?

You can use mtabulate in the following way: library(qdapTools) cbind(data[1], ...READ MORE

answered Apr 13, 2018 in Data Analytics by CodingByHeart77
• 3,740 points
2,321 views
+1 vote
2 answers

Custom Function to replace missing values in a vector with the mean of values

Try this. lapply(a,function(x){ifelse(is.na(x),mean(a,na.rm = TRUE ...READ MORE

answered Aug 14, 2019 in Data Analytics by anonymous
1,648 views
+1 vote
2 answers

Finding number of missing values and removing those missing values from a data-frame

To find number of missing values for ...READ MORE

answered Aug 14, 2019 in Data Analytics by anonymous
874 views
0 votes
1 answer

R: Sample from a neighborhood according to scores

I would suggest you to use the truncated ...READ MORE

answered May 29, 2018 in Data Analytics by Sahiti
• 6,370 points
372 views
0 votes
1 answer

How to sample random rows in dataframe?

Create data frame and then implement as ...READ MORE

answered Jul 3, 2018 in Data Analytics by Sahiti
• 6,370 points
616 views
0 votes
1 answer

How to sample n random rows per group in a dataframe?

You can assign a random ID to ...READ MORE

answered Jul 3, 2018 in Data Analytics by Sahiti
• 6,370 points
4,716 views
0 votes
0 answers

100 samples of 20 from the dataset and drawing regression lines along with population regression line

I have a datasetwith two variables hours ...READ MORE

Apr 11, 2022 in Machine Learning by Dev
• 6,000 points
477 views
0 votes
1 answer

How to one hot encode several categorical variables in R

43 I advise employing the caret package's dummyVars ...READ MORE

answered Jun 24, 2022 in Data Analytics by Sohail
• 3,040 points
607 views
0 votes
1 answer

Use of $ and %% operators in R

According to help('percent in percent'), percent in ...READ MORE

answered Jun 23, 2022 in Data Analytics by Sohail
• 3,040 points
440 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP