Replace empty values with value from other column in a dataframe

后端 未结 3 1033
说谎
说谎 2020-11-30 11:57

In one column of my dataframe I have some empty cells. The data looks like this:

   yymmdd    lat   lon mag depth knmilocatie baglocatie   tijd
 19861226 52.         


        
相关标签:
3条回答
  • 2020-11-30 12:24

    Or you can use a support vector:

    empty <- df$baglocatie == ""
    df$baglocatie[empty] <- df$knmilocatie[empty]
    
    0 讨论(0)
  • 2020-11-30 12:29

    Try ifelse:

    df$baglocatie <- ifelse(df$baglocatie == "", df$knmilocatie, df$baglocatie)
    
    0 讨论(0)
  • 2020-11-30 12:36

    You need to index also the replacing column:

    df[ df$baglocatie == "", "baglocatie"  ]  <- df[ df$baglocatie == "", "knmilocatie" ]
    
    0 讨论(0)
提交回复
热议问题