如何使用R根据匹配查找数据帧替换数据帧的多个列中的因子水平

Jai_surf_code

df1中与数据帧lookup_df中的lab_pt匹配的级别,我想替换为lookup_df第二栏中的相应级别(此处为lab_en)。但我想保持其余的不变。非常感谢!

--

主数据框

df1 <- data.frame(
            num_var = sample(200, 15),
            col1 = rep(c("onda","estrela","rato","caneta","ceu"), 3),
            col2 = rep(c("muro","gato","pa","rato","ceu"), 3),
            col3 = rep(c("surf","onda","dente","onda","sei"), 3),
            col3 = rep(c("onda","casa",NA,"nao","net"), 3))

查找数据帧

lookup_df <- data.frame(
            lab_pt = c("onda","estrela","rato","caneta","ceu"),
            lab_en = c("wave","star","rat","pen","sky"))

我已经在下面尝试过了。它可以完成工作,但是不匹配的信息会转换为NA,这是我不想要的。

rownames(lookup_df) <- lookup_df$lab_pt
apply(df1[,2:ncol(df1)], 2, function(x) lookup_df[as.character(x),]$lab_en)

这里的帖子非常相似,但是在这种情况下,所有级别都是可匹配的,与此不同。非常感谢!根据查找表替换数据框中的值

Jai_surf_code
# Fake dataframe
df1 <- tibble(
        num_var = sample(200, 15),
        col1 = rep(c("onda","estrela","rato","caneta","ceu"), 3),
        col2 = rep(c("muro","gato","pa","rato","ceu"), 3),
        col3 = rep(c("surf","onda","dente","onda","sei"), 3),
        col4 = rep(c("onda","casa",NA,"nao","net"), 3))

# Lookup dictionary dataframe
lookup_dat <- tibble(
        lab_pt = c("onda","estrela","rato","caneta","ceu"),
        lab_en = c("wave","star","rat","pen","sky")) 

#******************************************************************
#
# Translation by replacement of lookup dictionary 
# Developed to generate Rmd report with labels of plots in different languages
replace_level <- function(df, lookup_df, col_langu_in, col_langu_out){
        library(data.table)
        # function to replace levels in the df given a reference list in 
        # another df when level match it replace with the correspondent 
        #level in the same row name but in other column.
        # !!!! Variables col_langu need to be quoted 
           # 1) Below it creates a dictionary style with the reference df (2cols)
         lookup_vec <- setNames(as.character(lookup_df[[col_langu_out]]), 
                               lookup_df[[col_langu_in]])
           # 2) iterating over main df col names
         for (i in names(df)) { # select cols?: names(df)[sapply(df, is.factor)]
           # 3) return index of levels from df levels matching with those from 
                 # the dictionary type to replace (for each cols of df i)
                 if(is.character(df[[i]])){df[i] <- as.factor(df[[i]])}
                 # Changing from character to factor before the translation
                 index_match <- which(levels(df[[i]]) %in% 
                                              names(lookup_vec))
           # 4) replacing matchable levels based on the index on step 3).
                 # with the reference to translate
                 levels(df[[i]])[index_match] <- 
                         lookup_vec[levels(df[[i]])[index_match]]}
         return(df)}

# test here
replace_level(df1, lookup_dat, "lab_pt", "lab_en")

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何根据R中的因子水平计算数据帧中值的频率?

使用apply()函数更新R中数据帧的多列的因子水平

R-如何从多个匹配项中替换字符串(在数据帧中)

如何在R中查找和替换数据帧的String列值

如何根据另一个数据帧中定义的行/列缩放因子缩放 Pandas 数据帧?

R-根据多个条件匹配2个数据帧中的值(当查找ID的顺序是随机的时)

合并数据帧中的匹配因子

如何根据R中向量的值转换数据帧的列?

如何根据来自同一数据帧的多个列中的多个条件在数据帧中创建新列

遍历数据帧中的列以替换数据帧列表中匹配数据的值

如何基于R中的多个列聚合数据帧?

使用 R 中另一个数据帧的其他匹配 ID 替换数据帧中的值

根据R中的条件乘并替换数据帧中的值

从其他数据帧的查找中替换数据帧中的列

使用 NA 按因子折叠 r 中的数据帧

如何根据列和施加条件匹配数据帧?

多个条件使用 df 中的元素匹配查找表中的列名以合并 3 个数据帧

根据第二个数据帧替换数据帧中的列

根据来自R中不同大小的数据帧的多个条件匹配值

R - 替换多个数据帧中列名中的模式

R查找数据帧的不匹配列名

(因子)数据帧列的小写

Fuzzywuzzy在Python中匹配来自不同数据帧的多个列

如何在 R 中匹配具有多个 ID 的多个数据帧

如何在具有不同因子水平的不同数据帧中的函数中使用Forcats :: Fct_Collapse

根据R中另一个数据帧中给出的条件,用NA替换数据帧中的多个值

R 替换数据帧中多列中的特定值

根据 R 中数据帧中另一列的条件过滤数据帧

如何替换r中数据帧中多行中的值?