Home > Software design >  Problem while computing class = case_when in R, how to solve it?
Problem while computing class = case_when in R, how to solve it?

Time:12-03

So here is the dput(head(temp)) of my dataset:

temp <- structure(list(X = c("CZL", "CZL", "CZL", "CZL", "CZL", "FNR"
), database.code = c("CZL.BB", "CZL.CA", "CZL.EU", "CZL.RN",
"CZL.SL", "FNR.10_10"), tmt.plot.id = c("Picea", "long-lived",
"Picea", "long-lived", "Fagus", "long-lived"), dom.species = c("abies",
"broadleaves", "abies", "broadleaves", "sylvatica", "broadleaves"
), shr.of.ba = c(57.88029, 70.35056, 100, 85.16742, 65.68771,
11.24247), gini.total = c(0.678242, 0.6553011, 0, 0.6493601,
0.6144469, 0.4811145), gini.ctgr.total = c(2L, 2L, 1L, 2L, 2L,
1L), gini.dom = c(0.5821508, 0.5511097, 0.5555176, 0.6287997,
0.6425109, 0.2670409), gini.ctgr.dom = c(2L, 2L, 2L, 2L, 2L,
1L), species = c("multiple", "multiple", "single", "single",
"multiple", "multiple"), latitude = c(48.97861, 48.65528, 50.06833,
48.67861, 49.40194, 46.13386), longitude = c(13.8119444, 16.9422222,
17.2622222, 16.9488889, 18.4213889, -0.4346595)), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6"))
X database.code tmt.plot.id            dom.species shr.of.ba gini.total gini.ctgr.total
1 1           CZL      CZL.BB            Picea abies  57.88029  0.6782420               2
2 2           CZL      CZL.CA long-lived broadleaves  70.35056  0.6553011               2
3 3           CZL      CZL.EU            Picea abies 100.00000  0.0000000               1
4 4           CZL      CZL.RN long-lived broadleaves  85.16742  0.6493601               2
5 5           CZL      CZL.SL        Fagus sylvatica  65.68771  0.6144469               2
6 6           FNR   FNR.10_10 long-lived broadleaves  11.24247  0.4811145               1
   gini.dom gini.ctgr.dom  species latitude  longitude
1 0.5821508             2 multiple 48.97861 13.8119444
2 0.5511097             2 multiple 48.65528 16.9422222
3 0.5555176             2   single 50.06833 17.2622222
4 0.6287997             2   single 48.67861 16.9488889
5 0.6425109             2 multiple 49.40194 18.4213889
6 0.2670409             1 multiple 46.13386 -0.4346595

And here is what I am trying to pass:

temp <- temp %>% mutate(class = dplyr::case_when(gini.total >= 0.5 & gini.dom >= 0.5 & species == "single" ~ "single species irregular",
                                    gini.total >= 0.5 & gini.dom < 0.5 & species == "single" ~ "single species admixture",
                                    gini.total >= 0.5 & gini.dom >= 0.5 & species == "multiple" ~ "multiple species irregular",
                                    gini.total >= 0.5 & gini.dom < 0.5 & species == "multiple" ~ "multiple species layered",
                                    gini.total < 0.5  & species == "single" ~ "single species regular",
                                    gini.total < 0.5  & species == "multiple" ~ "multiple species regular", TRUE ~ NA_real_))

And I just get this error, which does not explain where or why it occurs:

Error in `mutate()`:
! Problem while computing `class = dplyr::case_when(...)`.
Caused by error in `dplyr::case_when

Does anyone know what I am doing wrong?

CodePudding user response:

Try changing NA_real_ to NA_character_. The error message admittedly is quite sparse, it should say something like: "Can't mix REAL with CHARACTER in that column."

temp %>% 
  mutate(class = 
    case_when(gini.total >= 0.5 & gini.dom >= 0.5 & species == "single" ~ 
      "single species irregular", 
    gini.total >= 0.5 & gini.dom < 0.5 & species == "single" ~ 
      "single species admixture", 
    gini.total >= 0.5 & gini.dom >= 0.5 & species == "multiple" ~ 
       "multiple species irregular", 
    gini.total >= 0.5 & gini.dom < 0.5 & species == "multiple" ~ 
       "multiple species layered", 
    gini.total < 0.5  & species == "single" ~ 
       "single species regular", 
    gini.total < 0.5  & species == "multiple" ~ 
       "multiple species regular", TRUE ~ NA_character_))
    X database.code tmt.plot.id dom.species shr.of.ba gini.total
1 CZL        CZL.BB       Picea       abies  57.88029  0.6782420
2 CZL        CZL.CA  long-lived broadleaves  70.35056  0.6553011
3 CZL        CZL.EU       Picea       abies 100.00000  0.0000000
4 CZL        CZL.RN  long-lived broadleaves  85.16742  0.6493601
5 CZL        CZL.SL       Fagus   sylvatica  65.68771  0.6144469
6 FNR     FNR.10_10  long-lived broadleaves  11.24247  0.4811145
  gini.ctgr.total  gini.dom gini.ctgr.dom  species latitude  longitude
1               2 0.5821508             2 multiple 48.97861 13.8119444
2               2 0.5511097             2 multiple 48.65528 16.9422222
3               1 0.5555176             2   single 50.06833 17.2622222
4               2 0.6287997             2   single 48.67861 16.9488889
5               2 0.6425109             2 multiple 49.40194 18.4213889
6               1 0.2670409             1 multiple 46.13386 -0.4346595
                       class
1 multiple species irregular
2 multiple species irregular
3     single species regular
4   single species irregular
5 multiple species irregular
6   multiple species regular
  •  Tags:  
  • r
  • Related