简体   繁体   中英

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.

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

Or you can try the map function from purrr package

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(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 . Pass the variables and the corresponding columns as input, replace the non-NA elements in the columns with the corresponding column names

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.

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