Creating groups based on arguments for a column


Currently in internship and novice on Studio R, I encounter difficulties in sorting my data.

I would like to sort my data according to the boat registrations into two categories: "small craft" and "trawler". I have a data bank for all small craft plates (81) but not for trawlers.

so I thought I would create the following condition:
if(any(ill$immatriculation == 859010&ill$immatriculation == 123463&...., "small craft", "twaler")

There’s no mistake, but it doesn’t change anything.
I also tried to create two tables but no conclusive results.

Can someone help me? Thank you in advance

Hi @AurianeS,

There are many here who are happy to help. But in order to receive help, you should provide a reproducible example. A reprex should provide the minimal amount of data and code necessary to reproduce the issue. If you cannot provide the actually data, you should try to provide some fake data that has the same structure.

To learn more about a reprex, check out the following link: FAQ: What's a reproducible example (`reprex`) and how do I do one?

1 Like

I'm sorry, so I built this simple data game. I know that my registrations: "123", "124" and "128" are "small trades".

So I want to build a table with all the data corresponding to the small fishermen. And let the other plates be in a trawler board.

So I thought:
small_trades <- data [data registration == 123 or data registration == 124 or data $ registration == 128]

The commade "or" does not exist, I have 81 license plates to write for this script and I do not know plates of registrations corresponding to the "trawler".


In R, "or" is the vertical pipe |, usually found above the return key on the keyboard. Based on the image of data you shown, try something like this:


# Small craft licenses
small_craft <- c('123', '124', '128')

data %>%
  mutate(small_trades = if_else(imatriculation %in% small_craft, 'small_craft', 'trawler'))

Thank you very much, the commade worked well, only the function seems "ephemeral". When I start my function I can observe a column with the corresponding data in my console.

But nothing appears in my data array, and I can not use the variable "small_trades"

dplyr doesn't perform in-place modifications, it creates a new data frame instead, if you want changes to persist, then you have to explicitly assign them to an object, for example.

data <- data %>%
  mutate(small_trades = if_else(imatriculation %in% small_craft, 'small_craft', 'trawler'))
1 Like

I had not thought about it, thank you for everything!!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.