简体   繁体   中英

R use purrr::map to apply a function on a selected columns in a dataframe list

I have a list of dataframes, where some columns need specification of their correct encoding. So, I have created a function to set the correct encoding, and I would like to apply this new function to the specific columns in my dataframes list. How can I do this with purrr:map ? I am very new to it.


Dummy example

# Set slovak characters
Sys.setlocale(category = "LC_ALL", locale = "Slovak")


# Make a function    
setEncoding<- function(x, ...) {
    Encoding(x)<-"UTF-8"  # set correct encoding on the vector
    x                     # print the output
}

# Create dummy data with wrong encoding
df1<-data.frame(name = "Ľubietovský Vepor",
                psb = "S CHKO PoÄľana",
                numb = 1)

df2<-data.frame(name = "Goliašová",
                psb = "S TANAP",
                numb = 2)

list1<-list(df1, df2)

My function seems working if applied on vector string:

>setEncoding(c("Ľubietovský Vepor", "Goliašová" ))
[1] "Ľubietovský Vepor" "Goliašová"  

# How to apply the whatever function (here setEncoding) on the selected columns from a dataframe list?? 

list1 %>%
  map(setEncoding[c("name", "psb")]) # How to fix this?

What I wish to obtain (correct encoding of columns name , psb ):

> ls
[[1]]
         name            psb numb
1 Ľubietovský Vepor S CHKO Poľana    1

[[2]]
         name     psb numb
1 Goliášová S TANAP    2

I don't know the details with encoding for your desired result, but I can answer the question regarding using purrr . You can use map_if to only apply the function to character vectors (since Encoding() expects a character input). Also your example data frames contain factors not strings.

library(purrr)
df1<-data.frame(name = "Ľubietovský Vepor",
                psb = "S CHKO PoÄľana",
                numb = 1, stringsAsFactors = FALSE)

df2<-data.frame(name = "Goliašová",
                psb = "S TANAP",
                numb = 2, stringsAsFactors = FALSE)

list1 <- list(df1, df2) #using ls conflicts with ls() function

list1 %>% 
  map_if(is.character, setEncoding) #this only maps on 'name' and 'pbs'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM