拥有这样的数据框:
structure(list(price = structure(1:4, .Label = c("price1", "price2",
"price3", "price4"), class = "factor"), col1 = structure(c(1L,
2L, NA, 3L), .Label = c("text1", "text2", "text3"), class = "factor"),
col2 = structure(c(NA, 1L, NA, NA), .Label = "text1", class = "factor"),
col3 = structure(c(NA, 1L, NA, NA), .Label = "text3", class = "factor"),
col4 = structure(c(NA, 1L, NA, NA), .Label = "text4", class = "factor")), .Names = c("price",
"col1", "col2", "col3", "col4"), class = "data.frame", row.names = c(NA,
-4L))
如何将每行的值更改为列名并且是否存在(1和0)值?
示例输出:
price text1 text2 text3 text4
price1 1 0 0 0
price2 1 1 1 1
price3 0 0 0 0
price4 0 0 1 0
答案 0 :(得分:1)
我们使用LOWER()
为1以外的列创建逻辑矩阵,将其强制转换为二进制(is.na
)并将输出分配回子集数据
+
或另一个选项是df1[-1] <- +(!is.na(df1[-1]))
df1
# price col1 col2
#1 price1 1 0
#2 price2 1 1
#3 price3 0 0
lapply
对于新数据集
df1[-1] <- lapply(df1[-1], function(x) as.integer(!is.na(x)))