【发布时间】:2016-07-07 12:49:47
【问题描述】:
我想用 0 或 1 填充数据帧(“DF”),具体取决于向量(“日期”)中的值是否与第二个数据帧(“df$Date”)中的其他日期值匹配。 如果它们匹配,则输出值必须为 1,否则为 0。
我尝试调整我朋友制作的这段代码,但它不起作用:
for(j in 1:length(Date)) { #Date is a vector with all dates from 1967 to 2006
# Start count
count <- 0
# Check all Dates between 1967-2006
if(any(Date[j] == df$Date)) { #df$Date contains specific dates of interest
count <- count + 1
}
# If there is a match between Date and df$Date, its output is 1, else 0.
DF[j,i] <- count
}
主数据框“DF”有 190 列,必须填充,当然还有等于日期向量的行数。
额外信息
1) 每一列都与其他列不同,因此一行中的观察值不能全部相等(即在单行中,我应该混合 0 和 1)。 2) "DF" 中的列名也以 df$Code 的形式出现在 "df" 中。
【问题讨论】:
-
也许
apply(MAT, 2, function(i) as.numeric(i == Date))