Home > database >  I want to create a R function which determines the mean height, mean, and coefficient of correlation
I want to create a R function which determines the mean height, mean, and coefficient of correlation

Time:01-13

I want to create a function correlateHeightSpeed() that would generate a data frame height_Speed from a list of data. then it shows the mean height, mean speed, and coefficient of correlation of height and speed for each type of roller coaster and each material of roller coaster. NA fields should be ignored in this.

the example dput data for main table looks like

dput(coaster_Data[1:10, c("name", "material_type", "seating_type","speed","height")])
structure(list(name = c("Goudurix", "Dream catcher", "Alucinakis", 
"Anaconda", "Azteka", "Bat Coaster", "Batman : Arkham Asylum", 
"Big Thunder Mountain", "EqWalizer", "Calamity Mine"), material_type = structure(c(1L, 
1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L), levels = c("Steel", "Wooden", 
"Hybrid", "na"), class = "factor"), seating_type = c("Sit Down", 
"Suspended", "Sit Down", "Sit Down", "Sit Down", "Inverted", 
"Inverted", "Sit Down", "Sit Down", "Sit Down"), speed = c(75, 
45, 30, 85, 55, 70, 80, 60, 76, 48), height = c(37, 25, 8, 35, 
17, 20, 32, 22, 36, 14)), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"))

I have tried making the data in top 10 higgest and i am getting the output correctly, But I need to do this for all the types of rollercoaster which is confusing.

subset_coaster <- data.frame(coaster_Data$name,coaster_Data$material_type,coaster_Data$speed)

CodePudding user response:

Try this

As you can see, you can adjust the groups you consider in the function "group_by"

    library(dplyr)
    Coaster_data %>% group_by(material_type) %>% mutate(Mean_Speed=mean(speed, na.rm=TRUE), 
                                                        Mean_Height=mean(height, na.rm=TRUE), 
                                                        Corr=cor(speed,height, use="complete.obs"))



# A tibble: 10 × 8
# Groups:   material_type [2]
   name                   material_type seating_type speed height Mean_Speed Mean_Height   Corr
   <chr>                  <fct>         <chr>        <dbl>  <dbl>      <dbl>       <dbl>  <dbl>
 1 Goudurix               Steel         Sit Down        75     37       59.9        23.4  0.835
 2 Dream catcher          Steel         Suspended       45     25       59.9        23.4  0.835
 3 Alucinakis             Steel         Sit Down        30      8       59.9        23.4  0.835
 4 Anaconda               Wooden        Sit Down        85     35       85          35   NA    
 5 Azteka                 Steel         Sit Down        55     17       59.9        23.4  0.835
 6 Bat Coaster            Steel         Inverted        70     20       59.9        23.4  0.835
 7 Batman : Arkham Asylum Steel         Inverted        80     32       59.9        23.4  0.835
 8 Big Thunder Mountain   Steel         Sit Down        60     22       59.9        23.4  0.835
 9 EqWalizer              Steel         Sit Down        76     36       59.9        23.4  0.835
10 Calamity Mine          Steel         Sit Down        48     14       59.9        23.4  0.835
  •  Tags:  
  • r
  • Related