Problems with conditioning a column's value from another column's RANGE

Hey guys,
I assume that this was asked already, but I was searching a lot and I haven't found any solution yet.
I do have the following data.frame

date     OCC2010          
 1992-1    8460               
 1992-1    7390               
 1992-1    9999               
 1992-2    244                
 1992-2    2349               
 1992-2    0592               

What I wanna do is actually pretty simple. I would like to allocate the value 3 to column SKILLOCC, if a row in column OCC2010 is within range of certain numbers, e.g. 8460:9999, as specified in the following table.

date     OCC2010          SKILLOCC
 1992-1    8460               4
 1992-1    7390               0
 1992-1    9999               4
 1992-2    244                0
 1992-2    2349               0
 1992-2    0592               0
df$SKILLEDOCC <- 0
df[df$OCC2010 == 8460:9999,]
df[df$OCC2010 == 8460:9999,]$SKILLEDOCC <- "3"

I used that code already for one value or a value < x and it worked. If I try it with a range, the error

> is not a multiple of the length of the connecting object
occurs. Does anyone have an idea how to fix that? Or is there another smarter way?

Many thanks in advance

Xx
Freddy

I think you want

df$SKILLEDOCC <- 0
df[df$OCC2010 >= 8460 & df$OCC2010 <= 9999,"SKILLEDOCC"] <- 3

or

df$SKILLEDOCC <- 0
df[df$OCC2010  %in% 8460:9999,"SKILLEDOCC"] <- 3

Using the comparison to the range, as you did, generates many TRUE/FALSE values at each row position and R does not know what to do with that. Try running

3 == 1:10

and remember that the boolean vector would be generated for each row.

1 Like
readr::read_csv("~/Desktop/grist.csv") -> a
#> 
#> ── Column specification ────────────────────────────────────────────────────────
#> cols(
#>   date = col_character(),
#>   OCC2010 = col_character(),
#>   SKILLOCC = col_double()
#> )
dat <- data.frame(date = c(
  "1992-1", "1992-1", "1992-1", "1992-2",
  "1992-2", "1992-2"
), OCC2010 = c(
  "8460", "7390", "9999", "244",
  "2349", "0592"
), SKILLOCC = c(4, 0, 4, 0, 0, 0))

dat
#>     date OCC2010 SKILLOCC
#> 1 1992-1    8460        4
#> 2 1992-1    7390        0
#> 3 1992-1    9999        4
#> 4 1992-2     244        0
#> 5 1992-2    2349        0
#> 6 1992-2    0592        0

span <- 8460:9999

check_entry <- function(x, y, z) which(x[,y] %in% z)
dat[,3][check_entry(dat,2,span)] <- 3

dat
#>     date OCC2010 SKILLOCC
#> 1 1992-1    8460        3
#> 2 1992-1    7390        0
#> 3 1992-1    9999        3
#> 4 1992-2     244        0
#> 5 1992-2    2349        0
#> 6 1992-2    0592        0

This works even though dat[,2] is typeof chr.

1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.