El Mexicano
El Mexicano

Reputation: 15

replace_na used on a data frame with columns of repeated names

I am trying to replace NA values within the columns of a data frame, but since some columns have identical names the function dplyr::replace_na replaces the NAs only for the first occurrence of each column name.

library(dplyr)
library(stringr)

namesvec1<-c("John Smith","John Smith Jr df", "Luis Rivera","Ricardo Feliciano  ADE","Huber Villa Gomez 12","Christian Pilares","Luis Rivera","Luis Rivera","Christian Pilares") 
namesvec<-c("John Smith", "Ricardo Feliciano","Christian Pilares","Luis Rivera","John Smith Jr")
namesvec<-sort(namesvec,decreasing = T)
namesfun<-(sapply(namesvec1, function (x)(str_extract(x,sapply(namesvec, function (y)y)))))%>%as.data.frame(stringsAsFactors = F)

mylist<-list()
for(i in 1:ncol(namesfun)){
  mylist[i]<-"zzz"

}
names(mylist)<-names(namesfun)
replace_na(namesfun,mylist)

The result i get is this:

enter image description here

Am I doing something wrong?

Upvotes: 1

Views: 91

Answers (2)

Aur&#232;le
Aur&#232;le

Reputation: 12819

One should never, ever, build data frames with duplicate column names. This is a source of horrendous bugs.

(Apologies for the strong language, but this is an absolute rule that suffers no exception).

Replace as.data.frame with data.frame (that uses make.names(unique = TRUE) internally to guarantee unicity of column names, as long as we keep the default check.names = TRUE).

The rest of the code will then work as expected.


(Or, possibly, come up with another data frame "shape" or data structure that is better suited to your needs, but this is hard to guess from the question alone).

Upvotes: 2

Jake Kaupp
Jake Kaupp

Reputation: 8072

I use purrr with replace_na to do what you want, when I have a common replacement value for NA.

library(tidyverse) #for tidyr/purrr/dplyr

map_df(namesfun, ~replace_na(.x, "zzz"))

# A tibble: 5 x 6
  `John Smith` `John Smith Jr df` `Luis Rivera` `Ricardo Feliciano  ADE` `Huber Villa Gomez 12` `Christian Pilares`
  <chr>        <chr>              <chr>         <chr>                    <chr>                  <chr>              
1 zzz          zzz                zzz           Ricardo Feliciano        zzz                    zzz                
2 zzz          zzz                Luis Rivera   zzz                      zzz                    zzz                
3 zzz          John Smith Jr      zzz           zzz                      zzz                    zzz                
4 John Smith   John Smith         zzz           zzz                      zzz                    zzz                
5 zzz          zzz                zzz           zzz                      zzz                    Christian Pilares  

Upvotes: 0

Related Questions