RStudio的新功能。我有一個有558行和4列的df。國家和城市在DF繼續重複。在R Studio中有558行和4列的df如何計算列中非數值的頻率
我想知道如何計算每個州有多少啤酒廠。這些列是:
Brew_ID Name City State
1 NorthGate Brewing Minneapolis MN
RStudio的新功能。我有一個有558行和4列的df。國家和城市在DF繼續重複。在R Studio中有558行和4列的df如何計算列中非數值的頻率
我想知道如何計算每個州有多少啤酒廠。這些列是:
Brew_ID Name City State
1 NorthGate Brewing Minneapolis MN
使用table()函數。
table(df$city)
給你每個城市的頻率計數。假設每行代表一個釀酒廠(並且您沒有州名重複的城市名稱),應該爲您提供每個城市釀酒廠的數量。
假設有沒有重複的行 - 你可以檢查此:
any(duplicated(df))
如果返回FALSE
那麼你可以使用table(df$State)
例如:
Brew_ID <- c(1,2,3,4,5,6,7,8,9,10)
Name <- c("NorthGate Bewing", "BrewDog", "BigBrew", "Hop Head", "Yadda", "Blah Brew", "LaLa brew", "Smith's", "Harold's", "Wendy's")
City <- c("Minneapolis", "New York", "Phoenix", "Sacremento", "Los Angeles", "San Francisco", "Portland", "Houston", "Dallas", "Austin")
State <- c ("MN", "NY", "AZ","CA", "CA", "CA", "OR", "TX", "TX", "TX")
df <- data.frame (Brew_ID, Name, City, State)
table(df$State)
返回:
AZ CA MN NY OR TX 1 3 1 1 1 3
如果你想要狀態vs Brew計數表:'table(df $ State,df $ Name)' – utubun