【问题标题】:reshape the data by adding 2 columns with通过添加 2 列来重塑数据
【发布时间】:2020-02-20 18:53:46
【问题描述】:

我有一个包含一些代码的文件,我想添加 2 列,其中包含一组固定值。我想输入:对于文件中的每个代码,在其中放置具有固定值集的 2 列。

在 R 中使用 Reshape 或 tidyr 或 dplyr。

enter image description here 例如: some_codes.csv 有: " 代码 123 234 r345 "

我想在这个(集合和类别)中创建 2 个具有固定值的列。

代码集类别 123 1个 123 2 乙 123 3 厘米 234 1个 234 2 乙 234 3 厘米 345 1个 345 2 乙 345 3 次

【问题讨论】:

标签: r dplyr reshape tidyr reshape2


【解决方案1】:

这里有一个解决方案。

对于每个代码,您将值和数据框绑定在一起。然后purrr::map_dfr 将通过绑定行将所有内容作为数据框连接在一起。

library(tidyverse)
codes = data.frame(codes=c(123,234,345))
categ = data.frame(set=1:3, categ=c("a","b","c"))
codes$codes %>% map_dfr(~{cbind(code=.x,categ)})

但正如 Tung 所说,您下次应该发布一个可重现的示例。您可以为此使用dput 函数。

【讨论】:

    猜你喜欢
    • 2019-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-12
    • 2016-09-02
    • 2021-08-12
    • 2018-07-15
    • 2021-09-18
    相关资源
    最近更新 更多