YJP
YJP

Reputation: 67

How to bring below column's value(factor) to other new column in R

I want to bring the same value from other column(factor) to a new column (in factor, I guess).

I get this error.

1: In `[<-.factor`(`*tmp*`, e, value = structure(10L, .Label = c("RSE7056",  ... :
  invalid factor level, NA generated

I've tried the below code

  for (e in 1:(a-1)) {
    if (data7$Freq[e]>1 && data7$RSE_ID[e] == data7$BEFORE_RSE_ID[e+1] && data7$NOW_COLCT_YMDHMS2[e] == data7$BEFORE_COLCT_YMDHMS2[e+1])  {
      data7$AFTER_RSE_ID[e] <- as.factor(data7$RSE_ID[e-1])
      data7$AFTER_time[e] <- 1
    }    
  }

I expect data7$AFTER_RSE_ID[e] be the same RSE~ as data7$RSE_ID[e-1] but it turns into three digit numbers.

The characters are as follows.

$ BEFORE_RSE_ID       : Factor w/ 404 levels "RSE1501","RSE1502",..: 309 160 160 159 166 188 169 183 188 169 ...
$ RSE_ID              : Factor w/ 26 levels "RSE7056","RSE7058",..: 7 10 10 7 26 8 13 12 17 14 ...
 $ AFTER_RSE_ID        : Factor w/ 26 levels "158","160","161",..: NA NA NA NA NA NA NA NA NA NA ...
result from the code I made

THE code I made

the whole result I get

THE result I want

THE result I want

Upvotes: 1

Views: 46

Answers (1)

akrun
akrun

Reputation: 887048

The error is due to the type of the column which is factor and when add some new values to that column, the levels should be preassigned or use a new factor column or easier would be to convert to character with as.character. Also, instead of the for loop, value comparisons with the next element can be done with lead

library(dplyr)
data7 %>%
    mutate_if(is.factor, as.character) %>% # change factor columns to character
    mutate(ind =Freq > 1 & 
               (RSE_ID == lead(BEFORE_RSE_ID,default = first(BEFORE_RSE_ID))) &
  (NOW_COLCT_YMDHMS2 == lead(BEFORE_COLCT_YMDHMS2, 
                     default = first(BEFORE_COLCT_YMDHMS2))),
       AFTER_RSE_ID = case_when(ind~ lag(RSE_ID), TRUE ~ RSE_ID),
      AFTER_time = as.integer(ind))

Upvotes: 1

Related Questions