How to combine multiple character columns into a single column in an R data frame

前端 未结 6 821
挽巷
挽巷 2020-12-03 01:10

I am working with Census data and I need to combine four character columns into a single column.

Example:

LOGRECNO STATE COUNTY  TRACT BLOCK
    60           


        
6条回答
  •  暖寄归人
    2020-12-03 01:54

    Or try this

    DF$BLOCKID <-
      paste(DF$LOGRECNO, DF$STATE, DF$COUNTY, 
            DF$TRACT, DF$BLOCK, sep = "")
    

    (Here is a method to set up the dataframe for people coming into this discussion later)

    DF <- 
      data.frame(LOGRECNO = c(60, 61, 62, 63, 64, 65),
                 STATE = c(1, 1, 1, 1, 1, 1),
                 COUNTY = c(1, 1, 1, 1, 1, 1), 
                 TRACT = c(21100, 21100, 21100, 21100, 21100, 21100), 
                 BLOCK = c(1053, 1054, 1055, 1056, 1057, 1058))
    

提交回复
热议问题