我有2个数据帧。 Df A有40行,Df B有50行。
每行对应一个美国州。每个都有两列:州名称和颜色。
我想创建一个新的矢量(colorvector),其中包含所有50个州的所有颜色。默认颜色应为dfB,但是如果状态存在于dfA中,我希望改为从此处获取颜色。
我尝试过:
update_member_variables
我得到了错误:
因子水平集不同
我尝试使dfA长50行,并用NA填充它,但这也不起作用。
对不起,如果我的格式已关闭!
答案 0 :(得分:0)
我认为您可以使用dplyr
以不同的方式完成此任务:
library(dplyr)
# Make sure none of your variables are factors
dfA <- dfA %>%
mutate_if(is.factor,as.character)
dfB <- dfB %>%
mutate_if(is.factor,as.character)
# Join data and change any of the NA color values from dfB to corresponding values from dfA
dfC <- left_join(dfA,dfB,by="State")
dfC$color.y <- ifelse(is.na(dfC$color.y),dfC$color.x, dfC$color.y)
# Create colorvector
colorvector <- as.vector(dfC$color.y)
dfA的dput:
dfA <- structure(list(State = c("Alabama", "Alaska", "Arizona", "Arkansas",
"California", "Colorado", "Connecticut", "Delaware", "Florida",
"Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa",
"Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts",
"Michigan", "Minnesota", "Mississippi", "Missouri", "Montana",
"Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico",
"New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma",
"Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota",
"Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington",
"West Virginia", "Wisconsin", "Wyoming"), color = c("color_Alabama",
"color_Alaska", "color_Arizona", "color_Arkansas", "color_California",
"color_Colorado", "color_Connecticut", "color_Delaware", "color_Florida",
"color_Georgia", "color_Hawaii", "color_Idaho", "color_Illinois",
"color_Indiana", "color_Iowa", "color_Kansas", "color_Kentucky",
"color_Louisiana", "color_Maine", "color_Maryland", "color_Massachusetts",
"color_Michigan", "color_Minnesota", "color_Mississippi", "color_Missouri",
"color_Montana", "color_Nebraska", "color_Nevada", "color_New Hampshire",
"color_New Jersey", "color_New Mexico", "color_New York", "color_North Carolina",
"color_North Dakota", "color_Ohio", "color_Oklahoma", "color_Oregon",
"color_Pennsylvania", "color_Rhode Island", "color_South Carolina",
"color_South Dakota", "color_Tennessee", "color_Texas", "color_Utah",
"color_Vermont", "color_Virginia", "color_Washington", "color_West Virginia",
"color_Wisconsin", "color_Wyoming")), class = c("tbl_df", "tbl",
"data.frame"), row.names = c(NA, -50L))
dfB的dput:
dfB <- structure(list(State = c("Alabama", "Alaska", "Arizona", "Arkansas",
"California", "Colorado", "Connecticut", "Delaware", "Florida",
"Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa",
"Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts",
"Michigan", "Minnesota", "Mississippi", "Missouri", "Montana",
"Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico",
"New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma",
"Oregon", "Pennsylvania", "Rhode Island", "South Carolina", NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA), color = c("color_Alabama",
"color_Alaska", "color_Arizona", "color_Arkansas", "color_California",
"color_Colorado", "color_Connecticut", "color_Delaware", "color_Florida",
"color_Georgia", "color_Hawaii", "color_Idaho", "color_Illinois",
"color_Indiana", "color_Iowa", "color_Kansas", "color_Kentucky",
"color_Louisiana", "color_Maine", "color_Maryland", "color_Massachusetts",
"color_Michigan", "color_Minnesota", "color_Mississippi", "color_Missouri",
"color_Montana", "color_Nebraska", "color_Nevada", "color_New Hampshire",
"color_New Jersey", "color_New Mexico", "color_New York", "color_North Carolina",
"color_North Dakota", "color_Ohio", "color_Oklahoma", "color_Oregon",
"color_Pennsylvania", "color_Rhode Island", "color_South Carolina",
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), row.names = c(NA, -50L
), class = c("tbl_df", "tbl", "data.frame"))