-1

I am trying to remove a group of columns from a data frame (followed this) but I get an error in return.

Specifically, size of the data frame (NNF.data) is 34233 rows with 147 columns:

[118] "NNF.2015.03.EUR"      "NNF.2015.04.EUR"      "NNF.2015.05.EUR"     
[121] "NNF.2015.06.EUR"      "NNF.2015.07.EUR"      "NNF.2015.08.EUR"     
[124] "NNF.2015.09.EUR"      "NNF.2015.10.EUR"      "NNF.2015.11.EUR"     
[127] "NNF.2015.12.EUR"      "NNF.2016.01.EUR"      "NNF.2016.02.EUR"     
[130] "NNF.2016.03.EUR"      "NNF.2016.04.EUR"      "NNF.2016.05.EUR"     
[133] "NNF.2016.06.EUR"      "NNF.2016.07.EUR"      "NNF.2016.08.EUR"     
[136] "YTD.NNF.Year2005.EUR" "YTD.NNF.Year2006.EUR" "YTD.NNF.Year2007.EUR"
[139] "YTD.NNF.Year2008.EUR" "YTD.NNF.Year2009.EUR" "YTD.NNF.Year2010.EUR"
[142] "YTD.NNF.Year2011.EUR" "YTD.NNF.Year2012.EUR" "YTD.NNF.Year2013.EUR"
[145] "YTD.NNF.Year2014.EUR" "YTD.NNF.Year2015.EUR" "YTD.NNF.Year2016.EUR"

What I want to do is to remove the columns from 136-147, or the ones that contain YTD in their name.

I tried to use

NNF.data[, grep("YTD", names(NNF.data)):= NULL]

but I get the error:

Error in `[.data.frame`(NNF.data, , `:=`(grep("YTD", names(NNF.data)),  : 
  could not find function ":="

Similarly, I tried

NNF.data[, which(grepl("YTD", colnames(NNF.data))):=NULL]

but again, I get

Error in `[.data.frame`(NNF.data, , `:=`(which(grepl("YTD", colnames(NNF.data))),  : 
  could not find function ":="

Any suggestions please? I made sure that NNF.data is a data frame

> is.data.frame(NNF.data)
[1] TRUE
Community
  • 1
  • 1
Jespar
  • 1,017
  • 5
  • 16
  • 29

2 Answers2

1

:= only works for data.table objects. If you are working with a data.frame you can try this:

df = data.frame(First = c(1,2,3), AVSecond = c(3,4,5), ThirdAV = c(6,7,8), Fourth = c(10,22,2))

df = df[-c(grep("AV", colnames(df)), 4)]

This will remove the columns with 'AV' in it and the Fourth column. Output:

  First
1     1
2     2
3     3
Carles Mitjans
  • 4,786
  • 3
  • 19
  • 38
  • `-grep(...)` is dangerous. Consider the case where no values are matched. You get `integer(0)`. – Rich Scriven Mar 24 '17 at 16:38
  • If no values are matched, it will remove only Fourth column. I'm not sure if you meant this or not – Carles Mitjans Mar 24 '17 at 16:41
  • 1
    Sure, but when that 4 isn't in there, you get no columns at all where you would want them all. – Rich Scriven Mar 24 '17 at 16:41
  • For example, try `df[-grep("xxx", colnames(df))]`. In this case of no matches, there are no columns to remove. You would want the whole data set returned, but you get nothing. `!grepl(...)` is safer. Or `!startsWith(...)`, as I mentioned in my comment on the question. – Rich Scriven Mar 24 '17 at 16:49
0
df = data.frame(YTD.NNF.Year2009.EUR=c(1,2,3),NNF.2016.06.EUR=c(3,4,5),HJK=c(6,7,8))
nm = colnames(df)  
numb = grepl("\\bYTD\\b", nm)
df = df[,-numb]
Aleksandr
  • 1,814
  • 11
  • 19