0

I am trying to write a function that returns all the observations in a specified data frame and column that have a number of letters < 5. For example if an observation was dogs it would be returned, but if another observation was store it would not be returned.

This is the code I have written:

count_length <- function(dataset, column_named){
  
  for (i in dataset$column_named)
    if (nchar(dataset$column_named[i] < 5)){
      paste("problem with ",i)
    }
  
  
}

count_length(dataset = df,
             column_named = pets)


The following error is returning:

Warning message:
Unknown or uninitialised column: `column_named`.

Does anyone know how to fix this and why it is happening? Is it related to the use of $ I wonder?

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
  • You should take a look at indexing in loops in R, it is not like in other languages. You likely need a `seq_along` the length of the column. Alternatively, you can use `*apply` and if you're feeling fancy, the `tidy` suite. – NelsonGon Feb 11 '22 at 14:09
  • Even if you fix the above, you can't index using character vectors using `$`, you should use `dataset[[column_named[i]]]`. – caldwellst Feb 11 '22 at 14:12

1 Answers1

0

You're right. It's indeed related to the use of $, as it is a function and cannot evaluate variables inside it.

You can try dataset$[column_named] for a dataframe or dataset$[[column_named]] for a vector

Longer explanation in this answer

Delta._.43
  • 70
  • 1
  • 8