[英]R Transpose A column into a Binary Row
I have a single data column that looks like this for "one record" There can be hundreds if not thousands of records where each record has a different ID but many different attributes ColName2 I want to transpose the table so it looks like table 2 Is this possible in R. It is possible in Rapid-miner but i would like to implement it in R 我有一个看起来像“一条记录”的数据列。可能有数百个(如果不是数千个)记录,其中每个记录具有不同的ID但有许多不同的属性ColName2我想转置该表,因此它看起来像表2在R中可能。在Rapid-miner中可能,但我想在R中实现
What I have Table 1 我有什么表1
What I want - Table 2 我想要的-表2
Thanks 谢谢
You can use reshape2
for this, for example: 您可以为此使用reshape2
,例如:
> df <- data.frame(ID = c(rep("1A", 4), "2A"), ColName = 1:5)
> df
# ID ColName
#1 1A 1
#2 1A 2
#3 1A 3
#4 1A 4
#5 2A 5
library(reshape2)
> df2 <- dcast(df, ID ~ ColName, fun.aggregate = any, value.var = "ColName")
The result of this reshapeing is: 重塑的结果是:
ID 1 2 3 4 5
1 1A TRUE TRUE TRUE TRUE FALSE
2 2A FALSE FALSE FALSE FALSE TRUE
So you have logical values (TRUE where you want 1 and FALSE where you want 0). 因此,您具有逻辑值(想要1时为TRUE,想要0时为FALSE)。 Since you can convert logical values to numeric, where TRUE == 1 and FALSE == 0, you just need to convert all columns (except the first) to numeric. 由于您可以将逻辑值转换为数字,其中TRUE == 1和FALSE == 0,因此您只需要将所有列(第一列除外)转换为数字。 To do this, you can use lapply
on the data.frame except the first column (indicated by df2[-1]) and apply the function as.numeric
to each of the other columns: 为此,可以对第一列(由df2 [-1]表示)之外的data.frame使用lapply
,并将函数as.numeric
应用于其他各列:
> df2[-1] <- lapply(df2[-1], as.numeric)
> df2
# ID 1 2 3 4 5
#1 1A 1 1 1 1 0
#2 2A 0 0 0 0 1
lapply
is often quite useful if you want to apply a function to all columns of a data.frame or all elements in a list. 如果要将函数应用于data.frame的所有列或列表中的所有元素, lapply
通常非常有用。 For some more information check out ?lapply
and this question . 有关更多信息,请查看?lapply
和此问题 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.