在R中子集数据帧后缀

时间:2014-10-16 14:23:23

标签: r subset

我是R的新手,我很难在下面的子集函数中删除带有-NWT后缀的那些:

我已经尝试了三条我在互联网上看过但仍然没有运气的线路:

trades.am <- subset(trades.am, Series.Name != "-NWT")
trades.am[trades.am$C != "-NWT", ]
sub.trades.am<-trades.am[trades.am[,3] != "-NWT",]
希望你们能提供帮助。

此致

1 个答案:

答案 0 :(得分:2)

尝试:

trades.am[!grepl("-NWT$", trades.am$Series.Name),]
#    Series.Name     value
#5    Something 1.6133728
#6    Something 0.0356312
#9    Something 0.8817912
#11   Something 0.9657529
#15   Something 1.9355718

更新

  vec1 <- c("FXTN 10-41*", "FXTN 90-21", "FXTN*")
  grepl("\\*$", vec1)
  # [1]  TRUE FALSE  TRUE

  vec1[!grepl("\\*$", vec1)]
  #[1] "FXTN 90-21"

数据

set.seed(42)
trades.am <- data.frame(Series.Name= sample(c("Something-NWT", "Something",
                      "Some-NWT"),15, replace=TRUE), value=rnorm(15))