如何创建一个包含日期和NA的数据框

时间:2019-08-07 15:58:38

标签: r dataframe

我有一个df 列= c(“ Tim”,“ Tom”,“ Peter”) 这些行显示特定的productID。

当Tim购买产品1时,df [1,1]应该类似于2019-08-01,而其余的df充满了NA。

我创建了带有一些值的df。日期值以18109之类的数字形式显示。我试图将其转换为日期为“ 2019-08-01”。

df[1,1:5] <- as.Date("18109", format = "%Y-%m-%d")
Error in as.Date.numeric(value) : 'origin' must be supplied

as.Date(KundenBestelldf$Tim, format = "%Y-%m-%d")
#results in a list of NAs and the date is deleted when I am looking for head()


df[1,1:5] <- as.Date("18109", format = "%Y-%m-%d")
#Error in as.Date.numeric(value) : 'origin' must be supplied

as.Date(df$Tim, format = "%Y-%m-%d")
#results in a list of NAs and the date is deleted when I am looking for 
head()

#the code to reproduce is:

#create customer vector
names <- c("Tim", "Tom", "Peter")
ID <- c(1:6)
names(ID) <- names

#matrix
matrix <- matrix(data = NA, nrow = 255, ncol = 6)

colnames(matrix) <- names

df <- data.frame(matrix)
class(df) #class is now data.frame

df[1,1:5] <- as.Date(as.integer("18109"),format = "%Y-%m-%d", origin = 
"1970-01-01")
#the class is actually numeric and now I can not transform it to date

0 个答案:

没有答案