【问题标题】:Using separate() to split date使用分开()分割日期
【发布时间】:2014-12-18 17:34:28
【问题描述】:

我有这个数据框:

Source: local data frame [446,604 x 2]

                  date pressure
    1  2014_01_01_0:01      991
    2  2014_01_01_0:02      991
    3  2014_01_01_0:03      991
    4  2014_01_01_0:04      991
    5  2014_01_01_0:05      991
    6  2014_01_01_0:06      991
    7  2014_01_01_0:07      991
    8  2014_01_01_0:08      991
    9  2014_01_01_0:09      991
    10 2014_01_01_0:10      991
    ..             ...      ...

我想将使用separate() 的日期列与tidyr 分开

library(tidyr)
separate(df, date, into = c("year", "month", "day", "time"), sep="_") 

但它不起作用。我设法使用substr()mutate() 做到了:

library(dplyr)
df %>%
mutate(
        year = substr(date,  1, 4),
        month = substr(date,  6, 7),
        day = substr(date, 9, 10),
        time = substr(date, 12, 15))

更新:

它不起作用,因为我的行格式不正确。我能够使用我最初的 substr() 方法进行诊断,我发现我在数据框中有奇怪的条目:

df %>%
  select(date) %>%

  mutate(
    year = substr(date,  1, 4),
    month = substr(date,  6, 7),
    day = substr(date, 9, 10),
    time = substr(date, 12, 15)) %>%

  group_by(year) %>%
  summarise(n=n())

这就是我得到的:

Source: local data frame [33 x 2]

   year      n
1  2014 446293
2  4164      9
3  4165     10
4  4166     10
5  4167     10
6  4168     10
7  4169     10
8  4170     10
9  4171     10
10 4172     10
11 4173     10
12 4174     10
13 4175     10
14 4176     10
15 4177     10
16 4178     10
17 4179     10
18 4180     10
19 4181     10
20 4182     10
21 4183     10
22 4184     10
23 4185     10
24 4186     10
25 4187     10
26 4188     10
27 4189     10
28 4190     10
29 4191     10
30 4192     10
31 4193     11
32 4194     10
33 4195      1

是否有更有效的方法来诊断列元素的结构并在执行分离()之前找到格式错误的行?

【问题讨论】:

  • 此代码separate(df, date, into = c("year", "month", "day", "time"), sep="_") 与您的问题一样,效果很好。确保您拥有当前版本的 tidyr。
  • 同意@docendodiscimus - separate 也为我工作。 dput(head(df)) 可能有助于调试。
  • 你也可以试试library(splitstackshape); cSplit(df, 'date', sep="_", type.convert=FALSE)
  • 注意:即使date 中的一行格式错误,separate 也无法很好地处理数据并抛出错误。它产生了什么错误?如果像 Error: Values ot split into… 这样的东西,那么它是一个格式错误的值。
  • 使用extra = mergeextra = drop

标签: r dplyr tidyr


【解决方案1】:

步骤如下:

  1. 先尝试separate()(无需额外)
  2. 请注意存在格式错误的行(控制台中的错误)
  3. 使用separate()extra = "drop"
  4. 使用group_by()summarise() 探索数据并确定要过滤掉的行

【讨论】:

    猜你喜欢
    • 2021-05-13
    • 2018-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    • 2010-11-26
    相关资源
    最近更新 更多