Convert from lowercase to uppercase all values in all character variables in dataframe

前端 未结 7 749
萌比男神i
萌比男神i 2020-12-05 09:17

I have a mixed dataframe of character and numeric variables.

city,hs_cd,sl_no,col_01,col_02,col_03
Austin,1,2,,46,Female
Austin,1,3,,32,Male
Austin,1,4,,27,         


        
7条回答
  •  春和景丽
    2020-12-05 09:41

    If you need to deal with data.frames that include factors you can use:

    df = data.frame(v1=letters[1:5],v2=1:5,v3=letters[10:14],v4=as.factor(letters[1:5]),v5=runif(5),stringsAsFactors=FALSE)
    
    df
        v1 v2 v3 v4        v5
        1  a  1  j  a 0.1774909
        2  b  2  k  b 0.4405019
        3  c  3  l  c 0.7042878
        4  d  4  m  d 0.8829965
        5  e  5  n  e 0.9702505
    
    
    sapply(df,class)
             v1          v2          v3          v4          v5
    "character"   "integer" "character"    "factor"   "numeric"
    

    Use mutate_each_ to convert factors to character then convert all to uppercase

       upper_it = function(X){X %>% mutate_each_( funs(as.character(.)), names( .[sapply(., is.factor)] )) %>%
       mutate_each_( funs(toupper), names( .[sapply(., is.character)] ))}   # convert factor to character then uppercase
    

    Gives

      upper_it(df)
          v1 v2 v3 v4
        1  A  1  J  A
        2  B  2  K  B
        3  C  3  L  C
        4  D  4  M  D
        5  E  5  N  E
    

    While

    sapply( upper_it(df),class)
             v1          v2          v3          v4          v5
    "character"   "integer" "character" "character"   "numeric"
    

提交回复
热议问题