-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
4
  • 1
    := works for data.table objects. Are you working with data.frame or data.table? Commented Mar 24, 2017 at 16:31
  • I didn't know that Carles, I am working with data.frame. Is there anything equivalent for data frame? Commented Mar 24, 2017 at 16:32
  • If there is, can you please post as an answer to upvote it? Thanks Commented Mar 24, 2017 at 16:32
  • 1
    NNF.data[startsWith(names(NNF.data), "YTD")] <- list(NULL), but data.table is more efficient. Commented Mar 24, 2017 at 16:35

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

4 Comments

-grep(...) is dangerous. Consider the case where no values are matched. You get integer(0).
If no values are matched, it will remove only Fourth column. I'm not sure if you meant this or not
Sure, but when that 4 isn't in there, you get no columns at all where you would want them all.
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.
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]

4 Comments

-numb probably won't work, since numb is a logical vector
numb is a logi [1:3] TRUE FALSE FALSE
Exactly. Now do - on that. I think you meant ! for negation.
So remove 1 column (2 and 3 leave) which is "YTD.NNF.Year2009.EUR" and it contains YTD so should be removed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.