我有一个数据框(df2),其中包含 Age、Info、Target 和 Info 转换为单热编码列,如下所示。
library(qdapTools)
require(reshape)
mydf <- structure(list(Age = c(99L, 10L, 40L, 15L),
Info = c("good, bad, sad", "nice, good, happy, joy", "NULL", "okay, nice, good, wild, go"),
Target = c("Boy", "Girl", "Boy", "Boy")),
.Names = c("Age", "Info", "Target"),
row.names = c(NA, 4L),
class = "data.frame")
df_with_ohe <- cbind(mydf, mtabulate(strsplit(mydf$Info, ", ")))
mydf
df_with_ohe
added_cols = names(df_with_ohe)[length(names(mydf))+1:length(names(df_with_ohe))]
[1] "bad" "go" "good" "happy" "joy" "nice" "NULL" "okay" "sad" "wild"
这是df2
Age Info Target bad go good happy joy nice NULL okay sad wild
1 99 good, bad, sad Boy 1 0 1 0 0 0 0 0 1 0
2 10 nice, good, happy, joy Girl 0 0 1 1 1 1 0 0 0 0
3 40 NULL Boy 0 0 0 0 0 0 1 0 0 0
4 15 okay, nice, good, wild, go Boy 0 1 1 0 0 1 0 1 0 1
我想绘制不同信息类型的年龄分布,即“好”、“坏”等的图。例如:
# considering on "good" column
df = subset(df_with_ohe, df_with_ohe[,"good"] == 1)
ggplot(df, aes(Target, Age)) + geom_boxplot()
如何 added_cols
在单个图中使用 One-Hot-Encoded 功能为所有列绘制这样的图?也许使用 Facet?