在grep表达式中,当grep的值为integer(0),打印"ok"时,我该怎么办?
> data="haha"
> grep("w",data)
integer(0)
> if (grep("w",data)==0) print ("ok")
Error in if (grep("w", data) == 0) print("ok") :
argument is of length zero
Run Code Online (Sandbox Code Playgroud) > x1=c(4,5,6,7,8)
> x1
[1] 4 5 6 7 8
> x2=x1[x1!=6]
> x2
[1] 4 5 7 8
> x3=x1[x1=6]
> x3
[1] NA
Run Code Online (Sandbox Code Playgroud)
为什么x3不是6?我不明白.
在Python中,我们可以使用raw_input(),我可以在R中使用什么?
>>> raw_input("let x=3 or 4?")
let x=3 or 4?3
'3'
Run Code Online (Sandbox Code Playgroud) 我有
str=c("00005.profit", "00005.profit-in","00006.profit","00006.profit-in")
Run Code Online (Sandbox Code Playgroud)
而且我想得到
"00005.profit" "00006.profit"
Run Code Online (Sandbox Code Playgroud)
如何grep在R中实现此目的?
> which(LETTERS=="A")
[1] 1
> which(LETTERS=="B")
[1] 2
Run Code Online (Sandbox Code Playgroud)
我可以使用一个语句来获得1,2的值吗?
which(LETTERS=="B" or "A")
Error: unexpected symbol in "which(LETTERS=="B" or"
Run Code Online (Sandbox Code Playgroud) contrast=list("1"="profit")
input=readline("please input")
please input1
input
[1] "1"
class(input)
[1] "character"
contrast[[input]]
[1] "profit"
contrast$"1"
[1] "profit"
contrast$input
NULL
Run Code Online (Sandbox Code Playgroud)
为什么 contrast$input 不等于 contrast$"1"?
输入值为"1",类也是字符.
eval(input)
[1] "1"
contrast$(eval(input))
Error: unexpected '(' in "contrast$("
contrast$eval(input)
Error: attempt to apply non-function
eval(paste(input))
[1] "1"
class(eval(paste(input)))
[1] "character"
contrast$eval(paste(input))
Error: attempt to apply non-function
contrast$(eval(paste(input)))
Error: unexpected '(' in "contrast$("
Run Code Online (Sandbox Code Playgroud)
有没有办法通过对比$ input获得价值?