Home > Back-end >  Why don't fix() or edit() work for my dataframe?
Why don't fix() or edit() work for my dataframe?

Time:01-23

I just want to recode manually answers of a string variable into another new variable the old school way (without using the DataEditR-pack or using regex() ). If I want to apply fix() or edit() for my df called "changes": I get following error:

Error in edit.data.frame(get(subx, envir = parent), title = subx, ...) : 
can only handle vector and factor elements 
4.
stop("can only handle vector and factor elements")
3.
edit.data.frame(get(subx, envir = parent), title = subx, ...)
2.
edit(get(subx, envir = parent), title = subx, ...)
1.
fix(changes)"

All other operations work fine, but I don't get it...

here is the output for str(changes)

tibble [6,007 x 5] (S3: tbl_df/tbl/data.frame)
 $ D009_01     : chr [1:6007] "" "" "" "" ...
  ..- attr(*, "format.spss")= chr "A751"
 $ Fall_ID     : num [1:6007] 1 2 3 4 5 6 7 8 9 10 ...
  ..- attr(*, "format.spss")= chr "F8.2"
 $ hochschule_f: Factor w/ 2 levels "UdS","htw": 1 1 1 1 1 1 1 1 1 1 ...
 $ D008        : dbl lbl [1:6007]  3,  2,  3,  3,  2,  3, NA,...
   ..@ label        : chr "Studienbedingungen während Studium geändert?"
   ..@ format.spss  : chr "F2.0"
   ..@ display_width: int 6
   ..@ labels       : Named num [1:4] -9 1 2 3
   .. ..- attr(*, "names")= chr [1:4] "nicht beantwortet" "Ja, verbessert." "Ja, verschlechtert." "Nein"
 $ D9_code     : num [1:6007] 0 0 0 0 0 0 0 0 0 0 ...

here for sessionInfo()

R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043)
Matrix products: default
locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252    LC_MONETARY=German_Germany.1252 LC_NUMERIC=C                    LC_TIME=German_Germany.1252    

>attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

>other attached packages:
 [1] shiny_1.7.1        DataEditR_0.1.4    knitr_1.36         summarytools_1.0.0 psych_2.1.9        magrittr_2.0.1     skimr_2.1.3        forcats_0.5.1     
 [9] stringr_1.4.0      purrr_0.3.4        readr_2.0.2        tidyr_1.1.4        tibble_3.1.5       ggplot2_3.3.5      tidyverse_1.3.1    stargazer_5.2.2   
[17] descr_1.1.5        car_3.0-12         carData_3.0-4      haven_2.4.3        dplyr_1.0.7       

>loaded via a namespace (and not attached):
 [1] nlme_3.1-153        matrixStats_0.61.0  fs_1.5.2            fontawesome_0.2.2   lubridate_1.8.0     httr_1.4.2          repr_1.1.3          bslib_0.3.1        
 [9] tools_4.1.2         backports_1.3.0     utf8_1.2.2          R6_2.5.1            DBI_1.1.2           colorspace_2.0-2    withr_2.4.2         tidyselect_1.1.1   
[17] mnormt_2.0.2        compiler_4.1.2      cli_3.1.0           rvest_1.0.2         xml2_1.3.3          shinyjs_2.1.0       rhandsontable_0.3.8 sass_0.4.0         
[25] scales_1.1.1        checkmate_2.0.0     digest_0.6.28       shinyBS_0.61        base64enc_0.1-3     pkgconfig_2.0.3     htmltools_0.5.2     dbplyr_2.1.1       
[33] fastmap_1.1.0       htmlwidgets_1.5.4   rlang_0.4.12        readxl_1.3.1        rstudioapi_0.13     pryr_0.1.5          jquerylib_0.1.4     generics_0.1.1     
[41] jsonlite_1.7.2      rapportools_1.0     Rcpp_1.0.7          munsell_0.5.0       fansi_0.5.0         abind_1.4-5         lifecycle_1.0.1     yaml_2.2.1         
[49] stringi_1.7.5       plyr_1.8.6          grid_4.1.2          parallel_4.1.2      promises_1.2.0.1    crayon_1.4.2        miniUI_0.1.1.1      lattice_0.20-45    
[57] pander_0.6.4        hms_1.1.1           magick_2.7.3        tmvnsim_1.0-2       pillar_1.6.4        tcltk_4.1.2         codetools_0.2-18    reprex_2.0.1       
[65] glue_1.4.2          modelr_0.1.8        vctrs_0.3.8         tzdb_0.2.0          httpuv_1.6.5        cellranger_1.1.0    gtable_0.3.0        assertthat_0.2.1   
[73] cachem_1.0.6        xfun_0.27           mime_0.12           xtable_1.8-4        broom_0.7.10        later_1.3.0         shinythemes_1.2.0   ellipsis_0.3.2```  

CodePudding user response:

Your dataframe contains some complicated columns. I think the D008 column is probably the one that can't be edited: it looks like an S4 object, and it looks as though fix() doesn't know what to do with it. So if you are hoping to edit that column, you are probably going to have to use some other method.

On the other hand, if you want to edit one of the other columns, you can probably do it by temporarily removing D008, doing the edit on the rest, and then restoring it afterwards. For example,

backup <- changes # save this in case something goes wrong

D008 <- changes[["D008"]]
changes[["D008"]] <- NULL

fix(changes)

changes[["D008"]] <- D008

The backup variable won't be needed unless there are some problems.

Edited to add: The above recipe didn't work, as the comments show. I tried to simulate your dataframe, and fix() failed on it. The following fixed mine:

attr(changes$Fall_ID, "format.spss") <- NULL
attr(changes$D009_01, "format.spss") <- NULL

If that's not enough, you may also need

changes <- as.data.frame(changes)

because it's a tibble, not really a dataframe.

  •  Tags:  
  • Related