tomas maldonado tomas maldonado - 1 month ago 6
R Question

R I cannot select specific values from a Variable that contains data like "01 1 5"

I have a dataframe like:

Tabla
IDrel ID_Sector
1 01 1 02
2 01 1 06
3 01SC


The
ID_Sector
is a factor, but I can transform it to character and
I want to filter the data, by selecting only some
ID_Sector
s.

I proved with

Tabla_1 <- sqldf("SELECT *
FROM Tabla
WHERE ID_Sector = '01SC'")


and gives me a dataframe with 0 observations

I proved also with:

Tabla_1 <- subset(Tabla, ID_Sector == "01 1")


and also gives me 0 observations.

I can with the SQLDF select an observation like "obs one", but I can`t make it with something like "1 0 1".

Answer

Maybe I'm misunderstanding your question, but does this do it for you?

library(dplyr)

Tabla <- data.frame(IDrel = 1:3, ID_Sector = as.factor(c("01 01 02", "01 01 06","01SC")))

And this gives:

Tabla

  IDrel ID_Sector
1     1  01 01 02
2     2  01 01 06
3     3      01SC

You can then change that to a character string, before obtaining the item in question:

Tabla$ID_Sector <- as.character(Tabla$ID_Sector)

Example <- Tabla %>% 
  filter(ID_Sector == "01SC")

This gives:

Example

  IDrel ID_Sector
1     3      01SC

Here's hoping that this helps you.

If not, then it sounds like you'll need to do some RegEx work on the string in question. For this, assuming that you're after the last word of this string, I'd use mutate from dplyr and word from stringr:

Tabla1 <- Tabla %>% 
  mutate(ID_Sector_LastWord = word(ID_Sector,start = -1))

Tabla

  IDrel ID_Sector ID_Sector_LastWord
1     1  01 01 02                 02
2     2  01 01 06                 06
3     3      01SC               01SC

You could then filter on ID_Sector_LastWord.