我不是 100% 清楚你需要你的最终结果#1 是什么......但如果你最终需要的是“种族”列来表示“西班牙裔或拉丁裔”,你可以这样做:
Data$Race[(Data$Ethnicity == "Hispanic or Latino")] <- "Hispanic or Latino"
您还可以像这样组合 Ethnicity 和 Race 列中的内容:
Data$Race[(Data$Ethnicity == "Hispanic or Latino")]<- paste(Data$Race[((Data$Ethnicity == "Hispanic or Latino")],Data$Ethnicity[(Data$Ethnicity == "Hispanic or Latino")])
对于#2...
#Load library
library(dplyr)
#Make test data
Data <- data.frame(Year = c(1990,1990,1991,1991),
State = c("AL", "MO", "AL", "MO"),
Population = c(1,2,2,3),
Race = c("Black", "Hispanic", "Hispanic", "Black"))
#Calculate total population
total_pop <- sum(Data$Population)
# Group by and calculate statistic, save to new 'df' dataframe
df <- Data %>%
group_by(Year, State, Race) %>%
summarise(percent = sum(Population)/total_pop)