r - 清除单独行 R 之后的组中除第一行之外的变量

标签 r tidyverse tail

我必须实现三件事:

  1. 按列分割的单独行:“日期”包含不同的年份。
  2. 除了列:“修复”,仅保留组内第一行有数据。
  3. 计算 Col:“价格”除以分割的行数。

    # Dataset call df 
    
    Name Fix Price   Date 
    Tom  600  500     2016-01-01
    John 800  400     2016-01-03;2016-01-09
    Mary 1100 1000    2016-01-04;2017-09-01;2017-08-10;2018-01-01
    Joe  30   25      2016-01-04;2017-09-01
    Paul 400  304     2015-01-02
    Alex 505  238     2018-01-02;2018-02-02
    
    # Targeted df
    
    Name Fix  Price   Date 
    Tom  600  500     2016-01-01
    John 800  400     2016-01-03;2016-01-09
    Mary 1100 250     2016-01-04
    Mary 0    250     2017-09-01
    Mary 0    250     2017-08-10
    Mary 0    250     2018-01-01
    Joe  30   12.5    2016-01-04
    Joe  0    12.5    2017-09-01
    Paul 400  304     2015-01-02
    Alex 505  238     2018-01-02;2018-02-02
    

我有一种方法可以实现 1 和 3 ,但我无法实现 2 ,因为我不知道 fill() 的 Diverse 函数。

# Find the SPLIT TARGET first :
inds <- sapply(strsplit(df$Date, ";"), function(x) 
length(unique(format(as.Date(x), "%Y"))) > 1) ### This approach actually 
does not works in my actual data when over 1 Million rows , i chunk it into a smaller data 
to fit this approach's limit.

library(tidyverse)
library(stringr)

#Select those indices 

df[inds, ] %>% mutate(Price = Price / (str_count(Date,";") + 1)) %>%
separate_rows(Date, sep = ";") %>%
bind_rows(df[!inds,])

*请提醒您不能使用 Col : "Name "来表示某些内容,因为它们仅表示数据集具有必须通过 separate_rows 复制的其他值
我怎样才能清除第2点?它让我发疯。感谢您的任何评论

最佳答案

dt = read.table(text = "
Name Fix Price   Date 
                Tom  600  500     2016-01-01
                John 800  400     2016-01-03;2016-01-09
                Mary 1100 1000    2016-01-04;2017-09-01;2017-08-10;2018-01-01
                Joe  30   25      2016-01-04;2017-09-01
                Paul 400  304     2015-01-02
                Alex 505  238     2018-01-02;2018-02-02
                ", header=T, stringsAsFactors=F)

library(tidyverse)
library(lubridate)

dt %>%
  separate_rows(Date, sep=";") %>%                     # separate dates
  group_by(Name, year = year(ymd(Date))) %>%           # for each Name and year of the date
  summarise(Fix = unique(Fix),                         # keep Fix
            Price = unique(Price),                     # keep Price
            Date = paste0(Date, collapse = ";")) %>%   # combine dates with same year
  mutate(Fix = ifelse(row_number() > 1, 0, Fix),       # update Fix values
         Price = Price/length(Price)) %>%              # divide Price by number of rows
  ungroup()                                            # forget the grouping

# # A tibble: 9 x 5
#   Name   year   Fix Price Date                 
#   <chr> <dbl> <dbl> <dbl> <chr>                
# 1 Alex   2018   505 238   2018-01-02;2018-02-02
# 2 Joe    2016    30  12.5 2016-01-04           
# 3 Joe    2017     0  12.5 2017-09-01           
# 4 John   2016   800 400   2016-01-03;2016-01-09
# 5 Mary   2016  1100 333.  2016-01-04           
# 6 Mary   2017     0 333.  2017-09-01;2017-08-10
# 7 Mary   2018     0 333.  2018-01-01           
# 8 Paul   2015   400 304   2015-01-02           
# 9 Tom    2016   600 500   2016-01-01 

关于r - 清除单独行 R 之后的组中除第一行之外的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50524932/

相关文章:

R:如何在图中的负轴上使用正确的减号

r - 使用其他列中的值跨多个列进行条件变异 - 在 tidyverse 中寻找有效的解决方案

r - Dplyr Grouped Mutate 有替代方案吗?

具有复合索引的分片集群上的 MongoDB Oplog 游标

linux - 在 dmesg 命令的输出之后

wireshark - 如何远程对 pcap 文件运行 tail?

R,从键值(哈希)获取键

r - 有条件地删除每列的值,但使用循环将每列保留为新数据框

r - 使用 ftransform 和来自折叠 R 包的 fgroup_by

r - 在函数中设置默认值?