我在dataframe = df中有一个列(runscored)它包含一些带*符号的数字我需要创建另一个列,如果runscored有* sign,则需要提及" notout "
。
df=
runscored
34
45
6
9
62*
55
70*
我想要这个:
runscored out/notout
34 out
45 out
6 out
9* notout
62* notout
55 out
70* notout
我试过这段代码
df$out/notout <- ifelse(grepl("*", df$Runs Scored, ignore.case = T), "notout","out")
但是新栏目只有
答案 0 :(得分:1)
如果您想搜索字符串*
,则需要在\\
前加*
,否则它将始终返回TRUE
。
df$`out/notout` <- with(df, ifelse(grepl("\\*", runscored), "notout", "out"))
输出为:
> df
runscored out/notout
1 34 out
2 45 out
3 6 out
4 9 out
5 62* notout
6 55 out
7 70* notout
示例数据:
df <- structure(list(runscored = c("34", "45", "6", "9", "62*", "55",
"70*"), `out/notout` = c("out", "out", "out", "out", "notout",
"out", "notout")), .Names = c("runscored", "out/notout"), row.names = c(NA,
-7L), class = "data.frame")