简体   繁体   English

将一年中的每一天的天数列转换为365个指标变量

[英]Convert column of day numbers to 365 indicator variables for each day of year

I have a data frame like the one generated below. 我有一个像下面生成的数据框。 The id column is not unique I also have a data frame of 365 columns, one for each day. id列不是唯一的,我还有一个365列的数据框,每天一列。 The "day" column represents the day of the year. “天”列代表一年中的一天。 I would like each row of the second data frame to map to the id column with 1s for the days that are present with that id and 0s otherwise. 我希望第二个数据框的每一行映射到id列的id对应于该id出现的日期,否则为0。 The data are not ordered neatly like in the example data frame. 数据没有像示例数据帧中那样整齐地排序。

data <- data.frame(id = 1:100, day = as.integer(runif(100, 0, 364)))

The expected output for an id that had the values 0, 3, and 364 in the original data frame would be: 在原始数据帧中ID为0、3和364的id的预期输出为:

    id day0 day1 day2 day3 ... day364
     1    1    0    0    1          1

I am also open to solutions that involve using data.table . 我也对涉及使用data.table解决方案data.table开放data.table Thank you! 谢谢!

Per my comment, you just want a crosstable, which can be obtained with: 根据我的评论,您只需要一个交叉表,可以通过以下方法获得它:

tab <- with(data,table(id, day))

If you just want that table to show presence/absence of matches (ie, a cell should be 1 even if multiple entries correspond to the same id/day pair), then just convert tab with the following: 如果您只是希望该表显示是否存在匹配项(即,即使多个条目对应于同一id / day对,则单元格也应为1),那么只需使用以下内容转换tab

tab[tab>=1] <- 1

Just for fun here's an alternate solution: 只是为了好玩,这里有一个替代解决方案:

library(reshape2)
dcast(data, id ~ day, is.integer, fill =0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM