【问题标题】:Create a new column with a specific string in every row在每一行中创建一个包含特定字符串的新列
【发布时间】:2020-12-07 13:51:39
【问题描述】:

在数据框中

df <-  data.frame(id = c(12,35), col1 = c("ext", "another"))

如何添加一个在每一行中都包含相同文本的新列

示例输出

df <-  data.frame(id = c(12,35), col1 = c("ext", "another"), mycol = c("mytext","mytext"))

【问题讨论】:

    标签: r


    【解决方案1】:

    以下应该这样做。

    df <-  data.frame(id = c(12,35), col1 = c("ext", "another"))
    
    df$mycol <-  "mytext"
    # id    col1  mycol
    # 1 12     ext mytext
    # 2 35 another mytext
    

    【讨论】:

      【解决方案2】:

      我们也可以利用回收

      data.frame(id = c(12,35), col1 = c("ext", "another"), mycol = "mytext")
      

      【讨论】:

        猜你喜欢
        • 2023-02-01
        • 2014-11-24
        • 2022-07-25
        • 1970-01-01
        • 2011-02-17
        • 2022-07-20
        • 1970-01-01
        • 2010-09-24
        • 1970-01-01
        相关资源
        最近更新 更多