1
我有一個數據框(df2),帶有兩個變量,Mood和PartOfTown,其中Mood是多選(即允許的選項的任意組合)問題評分人的快樂, PartOfTown描述了地理位置。在R中記錄逗號分隔條目
問題是,中心代碼的情緒不同,城市北部的中心使用NorthCode,南部的中心使用SouthCode(df1)。
我想將數據集(df2)中的所有條目重新編碼到SouthCode中,以便最終生成一個類似df3的數據集。我想要一個通用的解決方案,因爲可能會有新的組合,目前在數據集中不包含新的組合。任何想法都會非常感激。
中心代碼和定義的情緒:
df1 <- data.frame(NorthCode=c(4,5,6,7,99),NorthDef=c("happy","sad","tired","energetic","other"),SouthCode=c(7,8,9,5,99),SouthDef=c("happy","sad","tired","energetic","other"))
起點:
df2 <- data.frame(Mood=c("4","5","6","7","4,5","5,6,99","99","7","8","9","5","7,8","8,5,99","99"),Region=c("north","north","north","north","north","north","north","south","south","south","south","south","south","south"))
期望的結果:
df3 <- data.frame(Mood=c("7","8","9","5","7,8","8,9,99","99","7","8","9","5","7,8","8,5,99","99"),PartofTown=c("north","north","north","north","north","north","north","south","south","south","south","south","south","south"))
當前的嘗試:試圖通過拆分項目開始的,但無法讓它工作。
unlist(strsplit(df2$Mood, ","))