小编Ff *_* Yy的帖子

如何处理grep失败返回R中的整数(0)?

在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)

grep r return-value

11
推荐指数
1
解决办法
1万
查看次数

使用逻辑条件子集矢量

> 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?我不明白.

r

3
推荐指数
1
解决办法
1万
查看次数

在R中是否有一些函数如python中的raw_input()?

在Python中,我们可以使用raw_input(),我可以在R中使用什么?

>>> raw_input("let x=3 or 4?")
let x=3 or 4?3
'3'
Run Code Online (Sandbox Code Playgroud)

r

2
推荐指数
1
解决办法
3672
查看次数

如何在R中使用grep来获取指定的字符?

我有

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中实现此目的?

grep r

1
推荐指数
1
解决办法
3451
查看次数

使用多个条件子集矢量

> 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)

r which

1
推荐指数
1
解决办法
5376
查看次数

在R中获取list by name属性的值

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获得价值?

r

0
推荐指数
1
解决办法
3458
查看次数

标签 统计

r ×6

grep ×2

return-value ×1

which ×1