【问题标题】:Reshaping from Wide to Long using Year in Variable Name [duplicate]使用变量名称中的年份从宽到长重塑 [重复]
【发布时间】:2018-10-10 16:38:24
【问题描述】:

我正在尝试将大型数据框 (34645 x 11619) 从宽改成长。我想重塑 99 到 16 年。这意味着我有变量,例如“edu99”、“edu00”、...“edu 16”或变量,例如“p99d61”、“p00d61”、...、“p16d61 ”。年份字符串并不总是在同一位置。

有没有办法告诉 R 在重塑时在变量名中查找年份字符串“99-16”? (当然考虑到字符串数字唯一标识年份)。

或者一般来说,是否有有效的策略来重塑大数据集?

非常感谢您的帮助!

最好的,帕特里克

【问题讨论】:

  • 为了更好地获得有效答案,请 1) 出示您的代码; 2) 提供一个可重现的例子。

标签: r panel reshape


【解决方案1】:

我会使用 tidyr 而不是 reshape 这个:

  1. 使用gather()函数从宽到长:https://tidyr.tidyverse.org/reference/gather.html
  2. 使用extract()函数提取年份:https://tidyr.tidyverse.org/reference/extract.html

您可以在第 2 步中使用此正则表达式来提取年份 "(99|0[0-9]|1[1-9])"。它选择任何等于 99、介于 00 和 09 之间或介于 10 和 19 之间的数字对。

extract(<long_data_name>, <column_name>, <string_name_of_result_column>, regex = "(99|0[0-9]|1[1-9])", remove = TRUE)

【讨论】:

    猜你喜欢
    • 2011-03-20
    • 2021-08-26
    • 2018-05-05
    • 2018-02-17
    • 2019-11-18
    • 1970-01-01
    • 1970-01-01
    • 2021-04-30
    • 1970-01-01
    相关资源
    最近更新 更多