gpt4 book ai didi

r - 排除唯一日期

转载 作者:行者123 更新时间:2023-12-04 11:35:55 25 4
gpt4 key购买 nike

我在excel中有这样的数据并扩展到更多(Date5,Date6 ....)

 Date1   Value1 Date2    Value2 Date3    Value3 Date4   Value4
1/2/2004 17 1/3/2004 27 1/1/2004 17 1/3/2004 31
1/3/2004 26 1/4/2004 30 1/3/2004 29 1/4/2004 36
1/4/2004 22 1/5/2004 22 1/4/2004 28 1/5/2004 33
1/5/2004 17 1/6/2004 28 1/5/2004 36 1/6/2004 50
1/13/2004 15 1/7/2004 17 1/12/2004 15 1/8/2004 9
1/14/2004 10 1/14/2004 21 1/14/2004 12 1/14/2004 11

而且我想排除所有系列中不存在其关联日期的任何值。

对于我发布的示例数据,结果应如下所示:
Date    Value1  Value2  Value3  Value4
1/3/2004 26 27 29 31
1/4/2004 22 30 28 36
1/5/2004 17 22 36 33
1/14/2004 10 21 12 11

最佳答案

这是一个 dplyr/tidyr方法:

library(dplyr); library(tidyr)
gather(DF, key1, Date, -starts_with("Value")) %>%
gather(key2, Val, starts_with("Value")) %>%
filter(Date %in% Reduce(intersect, select(DF, starts_with("Date"))) &
gsub("[^0-9]", "", key1) == gsub("[^0-9]", "", key2)) %>%
select(-key1) %>% spread(key2, Val)

# Date Value1 Value2 Value3 Value4
#1 1/14/2004 10 21 12 11
#2 1/3/2004 26 27 29 31
#3 1/4/2004 22 30 28 36
#4 1/5/2004 17 22 36 33
#Warning:
#attributes are not identical across measure variables; they will be dropped

该警告与 factor 有关列被转换为 character .

——

在@AntoniosK 的评论后编辑

关于r - 排除唯一日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33647484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com