我正在尝试获取下表的虚拟变量:
df:
Value1 var1 var2 var3 var4
9.330154398 HomeATL AwayHOU HomeEast AwayWest
32.43881489 AwaySDN HomeATL HomeWest AwayWest
54.77178387 AwayLAN HomeATL AwayEast HomeSame
54.77178387 AwayLAN HomeATL AwayWest HomeEast
列var1
和var2
具有相同的级别。另一方面,列var3
和var4
也包含它们的级别。因此,我需要在创建虚拟变量期间,创建的新列不应具有重复级别。我的意思是,在var3和var4的示例中,第一行和第三行都具有AwayWest
,所以我只需要在名为AwayWest
的1列中每行填充数字1。
我想要的输出是:
Value1 HomeEast HomeWest AwayEast AwayWest HomeSame HomeATL AwayHOU AwaySDN AwayLAN
9.330154398 1 0 0 1 0 1 1 0 0
32.43881489 0 1 0 1 0 1 0 1 0
54.77178387 0 0 1 0 1 1 0 0 1
54.77178387 1 0 0 1 0 1 0 0 1
我尝试为要转换的每一列创建新的1列(col1
:
spread(df,var1, col1) %>%
spread(var2, col1)%>%
spread(var3, col1)%>%
spread(var1, col1)
但是它不起作用。
谢谢
答案 0 :(得分:3)
R的基本选项是使用model.matrix
df <- cbind(df[, "Value1", drop = F], model.matrix(Value1 ~ . - 1, data = df))
df
# Value1 var1AwayLAN var1AwaySDN var1HomeATL var2HomeATL var3AwayWest
#1 9.330154 0 0 1 0 0
#2 32.438815 0 1 0 1 0
#3 54.771784 1 0 0 1 0
#4 54.771784 1 0 0 1 1
# var3HomeEast var3HomeWest var4HomeEast var4HomeSame
#1 1 0 0 0
#2 0 1 0 0
#3 0 0 0 1
#4 0 0 1 0
如有必要,我们可以使用
来固定列名names(df) <- sub("var\\d", "", names(df))
再现您的预期输出。
df <- read.table(text =
"Value1 var1 var2 var3 var4
9.330154398 HomeATL AwayHOU HomeEast AwayWest
32.43881489 AwaySDN HomeATL HomeWest AwayWest
54.77178387 AwayLAN HomeATL AwayEast HomeSame
54.77178387 AwayLAN HomeATL AwayWest HomeEast", header = T)
答案 1 :(得分:2)
base R
选项为table
tbl <- +(table(c(col(df1[-1])), unlist(df1[-1]) ) > 0)
cbind(df1[1], as.data.frame.matrix(tbl))
或使用tidyverse
library(tidyverse)
rownames_to_column(df1, 'rn') %>%
gather(key, val, var1:var4) %>%
count(rn, val) %>%
spread(val, n, fill = 0) %>%
select(-rn) %>%
bind_cols(df1[1], .)
df1 <- structure(list(Value1 = c(9.330154398, 32.43881489, 54.77178387,
54.77178387), var1 = c("HomeATL", "AwaySDN", "AwayLAN", "AwayLAN"
), var2 = c("AwayHOU", "HomeATL", "HomeATL", "HomeATL"), var3 = c("HomeEast",
"HomeWest", "AwayEast", "AwayWest"), var4 = c("AwayWest", "AwayWest",
"HomeSame", "HomeEast")), class = "data.frame", row.names = c(NA,
-4L))
答案 2 :(得分:0)
您也可以这样做-
> data.table::setDT(df)[,id:=1:.N]
> cbind(df[,.(Value1)],dcast(
melt(setDT(df)[, c(.(id=id), lapply(c("var1","var2","var3","var4"), function(x) paste0(x, get(x))))], id.vars="id"),
id ~ value,
length))
输出-
Value1 id var1AwayLAN var1AwaySDN var1HomeATL var2AwayHOU var2HomeATL var3AwayEast var3AwayWest var3HomeEast var3HomeWest
1: 9.330154 1 0 0 1 1 0 0 0 1 0
2: 32.438815 2 0 1 0 0 1 0 0 0 1
3: 54.771784 3 1 0 0 0 1 1 0 0 0
4: 54.771784 4 1 0 0 0 1 0 1 0 0