【问题标题】:Creating a New Column of True/False in R在 R 中创建一个新的 True/False 列
【发布时间】:2015-06-30 12:39:32
【问题描述】:

我正在尝试在我的数据集中创建一个新列。到目前为止,我已经将一个 JSON 文件导入到 R 中,其中有一列充满了不同的词(“紫色”、“红色”、“蓝色”等)。每个观察都有这些词的某种组合。我的目标是创建一个带有明显单词标题的新列(“紫色”、“红色”、“蓝色”等)。我希望该列具有 True 或 False,具体取决于观察结果是否显示该颜色。我尝试使用子集函数以及手动这样做,但是有超过 300 种不同的观察结果,这非常不方便。我非常感谢任何帮助!

例如:

Observations     Color
1                Blue
2                Red, Blue
3                Blue, Green
4                Purple
5                Yellow, Orange

现在我想要

Observations     Color       Red       Yellow        Orange    Blue
1                Blue        False     False         False     True
2                Red, Blue   True      False         False     True

这是我在这个网站上的第一个问题,如果有任何问题,我深表歉意。

【问题讨论】:

标签: r subset sapply


【解决方案1】:

您可以简单地遍历要创建的列名并使用grepl 来查找它们是否存在于Color 列中:

dat <- read.table(text="Observations     Color
1                Blue
                  2                Red,Blue
                  3                Blue,Green
                  4                Purple
                  5                Yellow,Orange", header=T, stringsAsFactors=F)
# I removed the space after the commas to facilitate the data.frame creation.

cols <- c("Red", "Yellow", "Orange", "Blue")

for (i in cols) dat[[i]] <- grepl(i, dat$Color)

结果:

> dat
  Observations         Color   Red Yellow Orange  Blue
1            1          Blue FALSE  FALSE  FALSE  TRUE
2            2      Red,Blue  TRUE  FALSE  FALSE  TRUE
3            3    Blue,Green FALSE  FALSE  FALSE  TRUE
4            4        Purple FALSE  FALSE  FALSE FALSE
5            5 Yellow,Orange FALSE   TRUE   TRUE FALSE

编辑:

如果您想要所有颜色的列,创建矢量的更好方法是 Robert 在 cmets 中提出的:

cols <- unique(unlist(strsplit(dat$Color, ",")))
#You might have to change from "," to ", " if you have white spaces after the commas
#or even ",\\s?" if they aren't always there.

新结果将是:

  Observations         Color   Red Yellow Orange  Blue Green Purple
1            1          Blue FALSE  FALSE  FALSE  TRUE FALSE  FALSE
2            2      Red,Blue  TRUE  FALSE  FALSE  TRUE FALSE  FALSE
3            3    Blue,Green FALSE  FALSE  FALSE  TRUE  TRUE  FALSE
4            4        Purple FALSE  FALSE  FALSE FALSE FALSE   TRUE
5            5 Yellow,Orange FALSE   TRUE   TRUE FALSE FALSE  FALSE

【讨论】:

  • 更新颜色:cols &lt;- unique(unlist(strsplit(dat$Color, ",", fixed = TRUE)))
  • @Robert 我没有注意到他说他想要每种颜色的列,谢谢。
【解决方案2】:

试试这样的:

example <- data.frame(colors=c("A,B", "A", "B", "F", "C", "C,G", "C", "D", "E", "F"),stringsAsFactors = F)
cols <- sort(unique(unlist(strsplit(example$colors, ",", fixed = TRUE))))
dummies= sapply(cols,function(co)grepl(co, example$colors))

          A     B     C     D     E     F     G
 [1,]  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE
 [2,]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
 [3,] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE
 [4,] FALSE FALSE FALSE FALSE FALSE  TRUE FALSE
 [5,] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE
 [6,] FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE
 [7,] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE
 [8,] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE
 [9,] FALSE FALSE FALSE FALSE  TRUE FALSE FALSE
[10,] FALSE FALSE FALSE FALSE FALSE  TRUE FALSE

【讨论】:

    【解决方案3】:

    基本的 R 解释比我用 dplyr 可以实现的要简单得多,但为了感兴趣,这里有一个 dplyr 解决方案:

    cols <- unique(unlist(strsplit(dat$Color, ",", fixed = TRUE)))
    dat %>% mutate_(.dots = sapply(cols, function(col) interp(~grepl(col, Color), col = col)))
    

    这是一种使用plyrmagrittr的方法:

    cols %>% 
      laply(grepl, dat$Color) %>%
      t %>%
      data.frame %>%
      setNames(cols) %>%
      cbind(dat, .)
    

    还有一个:

    dat %>% adply(1, . %$%
                    Color %>%
                    strsplit(",") %>%
                    extract2(1) %>%
                    factor(levels = cols) %>%
                    table %>%
                    is_greater_than(0))
    

    这利用了magrittr 允许您创建匿名函数链这一事实。

    【讨论】:

      猜你喜欢
      • 2022-11-18
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      • 1970-01-01
      • 1970-01-01
      • 2020-12-27
      • 2021-08-21
      • 2021-02-02
      相关资源
      最近更新 更多