How to skip a paste() argument when its value is NA in R

后端 未结 1 395
無奈伤痛
無奈伤痛 2020-12-10 17:18

I have a data frame with the columns city, state, and country. I want to create a string that concatenates: \"City, State, Country\". However, one of my cities does

相关标签:
1条回答
  • 2020-12-10 17:48

    The alternative is to just fix it up afterwards:

    gsub("NA, ","",dff$string)
    
    #[1] "Austin, Texas, United States"       
    #[2] "Knoxville, Tennessee, United States"
    #[3] "Salk Lake City, Utah, United States"
    #[4] "Prague, Czech Rep"   
    

    Alternative #2, is to use apply once you have your data.frame called dff:

    apply(dff, 1, function(x) paste(na.omit(x),collapse=", ") )
    
    0 讨论(0)
提交回复
热议问题