将列添加到数据帧,如果字符串与特定模式匹配,则返回1 [英] Add column to data frame which returns 1 if string match a certain pattern

查看:61
本文介绍了将列添加到数据帧,如果字符串与特定模式匹配,则返回1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在现有数据框中添加一列,以标识该行中的元素是否包含特定模式.

I want to add a column to an existing data frame which identifies if the element in that row contains a specific pattern.

我虽然打算使用transform()函数来做到这一点.使用iris数据集

I though about using the transform() function to do it. Using the iris dataset,

> head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

> tail(iris)
    Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
145          6.7         3.3          5.7         2.5 virginica
146          6.7         3.0          5.2         2.3 virginica
147          6.3         2.5          5.0         1.9 virginica
148          6.5         3.0          5.2         2.0 virginica
149          6.2         3.4          5.4         2.3 virginica
150          5.9         3.0          5.1         1.8 virginica

我想添加一列,以标识Species是否以字符串sa结尾.在regex中,我可以使用表达式.*(sa)标记正确的字符串.

I would like to add a column which on identifies if the Species end with the string sa. In regex I can use the expression .*(sa) to flag the right strings.

如果Speciessa结尾,如果0没有以0结尾,如何编写用1填充列的函数?

How can I write a function which does populate the column with 1 if the Species ends with sa and 0 if it doesn't?

推荐答案

关于

iris$check <- as.numeric(grepl(".*(sa)", iris$Species))

grepl返回一个逻辑向量(TRUE/FALSE),可以使用as.numeric轻松将其转换为1/0.

grepl returns a logical vector (TRUE/FALSE) which can easily be converted to 1/0 by using as.numeric.

也可以:

iris$check <- grepl(".*(sa)", iris$Species) + 0L

这篇关于将列添加到数据帧,如果字符串与特定模式匹配,则返回1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆