R:创建此表的优雅方式

R:创建此表的优雅方式,r,R,我正在寻找一种优雅的方法来创建此数据表: 我有一个我感兴趣的日期向量 > date [1] "2008-01-01" "2008-01-02" "2008-01-03" "2008-01-04" "2008-01-05" > mytable 2008-01-01 2008-01-02 2008-01-03 2008-01-04 2008-01-05 1 0 1 0 2 和维基百科的数据表(每

我正在寻找一种优雅的方法来创建此数据表:

我有一个我感兴趣的日期向量

> date
[1] "2008-01-01" "2008-01-02" "2008-01-03" "2008-01-04" "2008-01-05"
> mytable
2008-01-01 2008-01-02 2008-01-03 2008-01-04 2008-01-05
         1          0          1          0          2
和维基百科的数据表(每天的浏览量)

我想要的是一个包含我感兴趣的日期数据的表

> date
[1] "2008-01-01" "2008-01-02" "2008-01-03" "2008-01-04" "2008-01-05"
> mytable
2008-01-01 2008-01-02 2008-01-03 2008-01-04 2008-01-05
         1          0          1          0          2
有谁能给我一个如何优雅地做这件事的提示吗


以下是dput的输出:


我想使用
match
将是最简单的方法。您需要使用
as.character
才能将日期与更改的名称

#  Match dates in the names of 'changes' vector. No match gives NA
#  Using setNames we can return the object and set the names in one command
mytable <- setNames( changes[ match(as.character(date) , names(changes)) ] , date )

#  Change NA values to 0 (is this sensible? Does no data mean 0 views or was the data not available?)
mytable[ is.na(mytable) ] <- 0

mytable
#2008-01-01 2008-01-02 2008-01-03 2008-01-04 2008-01-05 
#         1          0          1          0          3 
#匹配“变更”向量名称中的日期。没有对手能给你机会
#使用setNames,我们可以在一个命令中返回对象并设置名称

mytable我想使用
match
将是最简单的方法。您需要使用
as.character
才能将日期与更改的名称

#  Match dates in the names of 'changes' vector. No match gives NA
#  Using setNames we can return the object and set the names in one command
mytable <- setNames( changes[ match(as.character(date) , names(changes)) ] , date )

#  Change NA values to 0 (is this sensible? Does no data mean 0 views or was the data not available?)
mytable[ is.na(mytable) ] <- 0

mytable
#2008-01-01 2008-01-02 2008-01-03 2008-01-04 2008-01-05 
#         1          0          1          0          3 
#匹配“变更”向量名称中的日期。没有对手能给你机会
#使用setNames,我们可以在一个命令中返回对象并设置名称

mytable添加
dput(更改)
dput(日期)
的输出。这会让你的数据看起来像什么,而不是所有的猜测。我在猜测一个向量和一个命名向量。添加
dput(更改)
dput(日期)
的输出。这会让你的数据看起来像什么,而不是所有的猜测。我在猜一个向量和一个命名向量。非常感谢!很好用!谢谢!很好用!