选择带有特殊字符的值

时间:2019-02-26 23:15:55

标签: r excel

  1. 如何选择以"-"开头并以"*"结尾的值?
  2. 如何选择一个不以"-"开头但以"*"结尾的值?

例如,我的数据如下所示:

 0.5, -0.4*, 1.8*, 2.5**, 0.8
  1. 我在excel中的数据包含方程式。将数据导入R时,如何仅导入值?

谢谢!

1 个答案:

答案 0 :(得分:0)

我们可以使用str_match()gsub()(语法略有不同)。

library(stringr)

data = c("0.5", "-0.4*","1.8*", "2.5**", "0.8","-0.5")

#1. Matched all starting with - and ending without a star
str_match(data,"^-[0-9\\.]+[^\\*]$")

#2. Meches all not starting with - but ending with a star
str_match(data,"^[^-][0-9\\.]+\\*$")
  1. 您可以另存为csv文件,并将其读入R。