0

我有一个数据框(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?

4

1 回答 1

1

这样的事情怎么样?

df <- do.call(rbind, apply(mydf, 1, function(x) cbind.data.frame(Age=x[1], 
                                  Type=unlist(strsplit(x[2], split=', ')), 
                                  Target=x[3])))
df$Age <- as.integer(as.character(df$Age))
ggplot(df, aes(Target, Age)) + 
   geom_boxplot() + 
   facet_wrap(~Type, scales = 'free')

在此处输入图像描述

于 2016-10-01T13:10:45.810 回答