我試圖從 R 中的組合資料集中找到兩列(sunshine_in_hours 和 AgeGroup_30_to_34)之間的相關性。但是,每次我嘗試運行 cor() 函式時,我都會得到這個錯誤:
Error in pmatch(use, c("all.obs", "complete.obs", "pairwise.complete.obs", :
object 'AgeGroup_30_to_34' not found
這是 dput(head) 代碼片段:
structure(list(Date = structure(c(18659, 18660, 18661, 18663,
18665, 18666, 18667, 18668, 18669, 18670, 18671, 18673, 18674,
18675, 18676, 18677, 18678, 18679, 18680, 18681, 18682, 18683,
18684, 18685, 18686, 18687, 18688, 18689, 18690, 18691), class = "Date"),
Year = c(2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021,
2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021,
2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021,
2021, 2021), Month = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3), AgeGroup_30_to_34 = c(0,
0, 0, 2, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0,
2, 0, 0, 1, 2, 0, 3, 0, 0, 0), Sunshine_in_hours = c(1.6,
3.4, 13.1, 8.9, 2, 1.7, 12.7, 11.6, 5.5, 5.6, 4.9, 9.2, 8.3,
11.9, 12.4, 12.4, 5.9, 0, 6.3, 8.5, 9.9, 8.7, 6.3, 1, 9.2,
6.3, 1.4, 2.1, 2.6, 3.6), City = c("Melbourne", "Melbourne",
"Melbourne", "Melbourne", "Melbourne", "Melbourne", "Melbourne",
"Melbourne", "Melbourne", "Melbourne", "Melbourne", "Melbourne",
"Melbourne", "Melbourne", "Melbourne", "Melbourne", "Melbourne",
"Melbourne", "Melbourne", "Melbourne", "Melbourne", "Melbourne",
"Melbourne", "Melbourne", "Melbourne", "Melbourne", "Melbourne",
"Melbourne", "Melbourne", "Melbourne")), row.names = c(NA,
-30L), class = c("tbl_df", "tbl", "data.frame"))
我試圖運行代碼:
Combined <- inner_join(covidS, weatherS, by = 'Date')%>%
mutate(Date = mdy(Date),
Year = year(Date),
Month = month(Date),
Day = day(Date))%>%
select(Date, Year, Month, AgeGroup_30_to_34, Sunshine_in_hours, City)%>%
filter(City == 'Melbourne')%>%
cor(Sunshine_in_hours, AgeGroup_30_to_34 )
我已經嘗試查找有關如何執行此操作的教程,但是我一直碰壁。任何幫助將不勝感激。
uj5u.com熱心網友回復:
cor
需要兩個輸入,你給它 3,其中兩個它不理解。嘗試這個:
Combined <- inner_join(covidS, weatherS, by = 'Date')%>%
mutate(Date = mdy(Date),
Year = year(Date),
Month = month(Date),
Day = day(Date))%>%
select(Date, Year, Month, AgeGroup_30_to_34, Sunshine_in_hours, City)%>%
filter(City == 'Melbourne')
corr = cor(Combined$Sunshine_in_hours, Combined$AgeGroup_30_to_34 )
請記住,當您使用管道時,您將最后一個物件作為您正在呼叫的函式的第一個引數提供。在這種情況下,您的代碼相當于:
cor(inner_join(covidS, weatherS, by = 'Date')%>%
mutate(Date = mdy(Date),
Year = year(Date),
Month = month(Date),
Day = day(Date))%>%
select(Date, Year, Month, AgeGroup_30_to_34, Sunshine_in_hours, City)%>%
filter(City == 'Melbourne'),
Sunshine_in_hours, AgeGroup_30_to_34 )
Sunshine_in_hours
因此,AgeGroup_30_to_34
如果函式不知道這些是來自另一個資料幀的列,那么兩者都沒有任何意義。問題是,這個函式是為基本 R 編碼的,而你的其余編程是dplyr
,它們是不同的范例。如有疑問,請務必檢查檔案
uj5u.com熱心網友回復:
使用magrittr
exposition 管道%$%
代替%>%
您可以執行以下操作:
library(magrittr)
dat %$%
cor(Sunshine_in_hours, AgeGroup_30_to_34)
#> [1] -0.0006941058
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/529062.html
標籤:r