如果变量不存在,则将变量添加到数据集

时间:2014-09-25 12:38:13

标签: r variables

在R中工作,我需要在缺失的日子里添加一列零 - 如果有的话。

我处理了许多包含日期的数据集。我找到每个日期的日期(day.ceceived),然后使用:

dcast(data, ... ~ day.received)

这将我的日子变成了列,并给出了适用的记录数。完美!

然而,我刚刚发现,如果我一天没有任何记录,那么当我播放时,我不会创建一个专栏。这使我的代码在整个地方抛出错误,因为我的数据帧不再是预期的长度,并且找不到对缺失日期的列的任何引用。

以下是一个例子:

set.seed(42)
days <- factor(c('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday','Saturday', 'Sunday'),
           c('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday','Saturday', 'Sunday'))
data <- data.frame(Place = sample(c('A','B','C'),replace = TRUE, 50), 
       day.received = sample(days, 50, replace=TRUE))
data$day.received[data$day.received == 'Friday'] <- "Saturday"
data <- dcast(data, ... ~ day.received)
data

显然我可以使用%in%来查找星期一到星期日是否在同名中,但是如何使用该信息为缺失的日子添加一列零?我可以计算出我需要添加的向量的长度,让我们说它是10.

2 个答案:

答案 0 :(得分:3)

不完全确定您需要什么,因为没有明确的例子,但您是否正在寻找以下内容:

days <- days.of.week <- weekdays(x=as.Date(seq(7), origin="1950-01-01"))
days

## [1] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"    "Saturday"  "Sunday"   

set.seed(1492) # make reproducible

# fabricated data frame with missing weekdays

data <- data.frame(
  Sunday=sample(1:100, 10),
  Tuesday=sample(1:100, 10),
  Thursday=sample(1:100, 10))

head(data, 3)

##   Sunday Tuesday Thursday
## 1     28      65       64
## 2     22      64       38
## 3     19      20       13

# fill in missing columns

data[, setdiff(days, colnames(data))] <- 0
head(data, 3)

##    Sunday Tuesday Thursday Monday Wednesday Friday Saturday
## 1      28      65       64      0         0      0        0
## 2      22      64       38      0         0      0        0
## 3      19      20       13      0         0      0        0

答案 1 :(得分:1)

您实际上只是错过了drop参数:

dcast(data, ... ~ day.received, drop = FALSE)
# Using day.received as value column: use value.var to override.
# Aggregation function missing: defaulting to length
#   Place Monday Tuesday Wednesday Thursday Friday Saturday Sunday
# 1     A      2       4         1        1      0        2      0
# 2     B      3       2         2        2      0        7      1
# 3     C      3       4         4        3      0        7      2