我想创建一个R函数来确定平均高度,平均值,以及高度和速度的相关系数

gstyhher  于 2023-01-15  发布在  其他
关注(0)|答案(1)|浏览(94)

我想创建一个函数correlateHeightSpeed(),它会从一个数据列表中生成一个 Dataframe height_Speed。然后它会显示每种类型的过山车和过山车的每种材料的平均高度、平均速度以及高度和速度的相关系数。
主表的示例dput数据如下所示

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"))

我已经尝试使数据在前10名higgest和我得到的输出正确,但我需要这样做的所有类型的过山车这是混乱的。

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

hc2pp10m1#

试试这个
如您所见,您可以在函数"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

相关问题