简体   繁体   English

For循环将所有非NA观测值重命名为R中的列名

[英]For loop to rename all non-NA observations to column name in R

I have a very messy data structure and I'm trying to reassign the name of any observation in a subset of columns to be that column name. 我的数据结构非常凌乱,我试图将列的子集中的任何观察值重新分配为该列名。 I want to write a for loop that says for all non.na observations in columns 28:141 (must call the columns by their number not their title), rename observation name to that columns name. 我想编写一个for循环,对列28:141中的所有non.na观察说(必须通过列的编号而不是其标题来调用列),将观察名称重命名为该列名称。

Example data structure 示例数据结构

df <- data.frame(Id = c('x111', 'x222', 'x333', 'x444'), 
                    Ext =c("M", "L", "S","S"),
                    Ind1 = c('clean', NA, NA, 'clean'),
                    Ind2 = c(NA, 'medium', 'medium', NA),
                    Ind3 = c(NA, NA, 'tall', NA),
                    Ext2 = c(12, 15, 11, 9))

For example, how to rename all non.na observations in columns 2:4 to that specific columns name 例如,如何将列2:4中的所有non.na观测值重命名为该特定列的名称

Or you can try the map function from purrr package 或者您可以从purrr包中尝试地图功能

df[2:4] <- purrr::map2_df(df[2:4],colnames(df[2:4]),function(x,y){
    ifelse(is.na(x),x,y)
})
> df
    Id Ext Ind1 Ind2 Ind3 Ext2
1 x111 Ext Ind1 <NA> <NA>   12
2 x222 Ext <NA> Ind2 <NA>   15
3 x333 Ext <NA> Ind2 tall   11
4 x444 Ext Ind1 <NA> <NA>    9

要么:

df[2:4] <- purrr::imap_dfc(df[2:4], function(x, y) ifelse(is.na(x), x, y))

Since you asked for a for loop (Note that for big data.frames this will be considerably slower): 由于您请求了for循环(请注意,对于大data.frames,这将相当慢):

for(i in 2:4){
  if(F %in% is.na(df[, i])){
    df[which(!is.na(df[, i])), i] <- names(df)[i]
  }
}

Which leaves us with: 这给我们留下了:

   Id Ext Ind1 Ind2 Ind3 Ext2
1 x111 Ext Ind1 <NA> <NA>   12
2 x222 Ext <NA> Ind2 <NA>   15
3 x333 Ext <NA> Ind2 tall   11
4 x444 Ext Ind1 <NA> <NA>    9

We can use Map from base R . 我们可以从base R使用Map Pass the variables and the corresponding columns as input, replace the non-NA elements in the columns with the corresponding column names 传递变量和相应的列作为输入, replace列中的非NA元素replace为相应的列名

df[2:4] <- Map(function(x, y) replace(as.character(x),
          !is.na(x), y), df[2:4], names(df)[2:4])

When we pass a data.frame as input, each column is a unit and when it is a vector ( names(df)[2:4] ), the unit is each of the element. 当我们将data.frame作为输入传递时,每一列都是一个单位,而当它是一个vectornames(df)[2:4] )时,该单位就是每个元素。

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

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