繁体   English   中英

在 dataframe 中创建列,R 中的字符串不存在

[英]create columns in dataframe with absent from string in R

我有一个 dataframe 和一个字符串。
我需要检查字符串中的元素是否在 dataframe 的列名中。
如果不在 dataframe 中,我需要新建一个列,
如果它们在 dataframe 中,什么也不做

这是我的代表:

# dataframe


df <- data.frame(name = c("jon", "dan", "jim"), 
                 age = c(44, 33, 33))

# string

st <- c("house", "car", "pet")


# for just one element in the string, this works  

df %>%
          mutate(house = if (exists('house', where = .)) house else "not there")  


however, my function to apply to multiple elements is not working.. any help much appreciated..


make_missing_cols <- function(df, cols){

          for(i in cols) {
              
                    df <- df %>%      
                              mutate(cols[i] = if(exists(cols[i], where = df)) cols[i] else "not there")      
      
          }
          return(df)
          
}


 

在 function 中,我们需要一个赋值运算符作为:=和评估 ( !! )

make_missing_cols <- function(df, cols){

          for(i in seq_along(cols) ){
           df <- df %>%      
             mutate(!!cols[i] := if(exists(cols[i], 
                    where = df)) cols[i] else "not there")      
          }
          return(df)
}

-测试

make_missing_cols(df, st)
#  name age     house       car       pet
#1  jon  44 not there not there not there
#2  dan  33 not there not there not there
#3  jim  33 not there not there not there

一个不同的选择可能是:

df %>%
 add_column(!!!setNames(rep("not there", length(setdiff(st, names(.)))), setdiff(st, names(.))))

 name age     house       car       pet
1  jon  44 not there not there not there
2  dan  33 not there not there not there
3  jim  33 not there not there not there

您可以在基础 R 中使用setdiff

make_missing_cols <- function(df, cols){
  df[setdiff(cols, names(df))] <- 'not there'
  df
}

make_missing_cols(df, st)

#  name age     house       car       pet
#1  jon  44 not there not there not there
#2  dan  33 not there not there not there
#3  jim  33 not there not there not there

make_missing_cols(df, c('house', 'name'))
#  name age     house
#1  jon  44 not there
#2  dan  33 not there
#3  jim  33 not there

make_missing_cols(df, 'name')

#  name age
#1  jon  44
#2  dan  33
#3  jim  33

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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