根据另一列中的正则表达式填充r中的列

时间:2018-02-26 20:31:49

标签: r regex tidyverse stringr

我在R中做了一些关于葡萄酒评论的数据争论,但却找不到一种优雅的方式来做我想做的事。
我的目标是查看通常包含葡萄酒年份的葡萄酒评论的标题栏,并将该年份放在不同的栏目中。 Kernal:https://www.kaggle.com/kieroneil/data-wrangling-wine-reviews-in-r

这是完成我想要的代码,但我希望有人能告诉我一个更好的方法:

# Create the year columns and assign an arbitrary value.
library(tidyverse)
wine_04$year <- 1900
year_2000 <- unlist(str_detect(wine_04$title, "2000"))
year_2001 <- unlist(str_detect(wine_04$title, "2001"))
year_2002 <- unlist(str_detect(wine_04$title, "2002"))
year_2003 <- unlist(str_detect(wine_04$title, "2003"))
year_2004 <- unlist(str_detect(wine_04$title, "2004"))
year_2005 <- unlist(str_detect(wine_04$title, "2005"))
year_2006 <- unlist(str_detect(wine_04$title, "2006"))
year_2007 <- unlist(str_detect(wine_04$title, "2007"))
year_2008 <- unlist(str_detect(wine_04$title, "2008"))
year_2009 <- unlist(str_detect(wine_04$title, "2009"))
year_2010 <- unlist(str_detect(wine_04$title, "2010"))
year_2011 <- unlist(str_detect(wine_04$title, "2011"))
year_2012 <- unlist(str_detect(wine_04$title, "2012"))
year_2013 <- unlist(str_detect(wine_04$title, "2013"))
year_2014 <- unlist(str_detect(wine_04$title, "2014"))
year_2015 <- unlist(str_detect(wine_04$title, "2015"))
year_2016 <- unlist(str_detect(wine_04$title, "2016"))
year_2017 <- unlist(str_detect(wine_04$title, "2017"))

wine_04[year_2000 == TRUE, 15] <- 2000
wine_04[year_2001 == TRUE, 15] <- 2001
wine_04[year_2002 == TRUE, 15] <- 2002
wine_04[year_2003 == TRUE, 15] <- 2003
wine_04[year_2004 == TRUE, 15] <- 2004
wine_04[year_2005 == TRUE, 15] <- 2005
wine_04[year_2006 == TRUE, 15] <- 2006
wine_04[year_2007 == TRUE, 15] <- 2007
wine_04[year_2008 == TRUE, 15] <- 2008
wine_04[year_2009 == TRUE, 15] <- 2009
wine_04[year_2010 == TRUE, 15] <- 2010
wine_04[year_2011 == TRUE, 15] <- 2011
wine_04[year_2012 == TRUE, 15] <- 2012
wine_04[year_2013 == TRUE, 15] <- 2013
wine_04[year_2014 == TRUE, 15] <- 2014
wine_04[year_2015 == TRUE, 15] <- 2015
wine_04[year_2016 == TRUE, 15] <- 2016
wine_04[year_2017 == TRUE, 15] <- 2017

感谢您的帮助。

0 个答案:

没有答案
相关问题