In R, how do I translate one record with a single comma-separated field into multiple records?

前端 未结 2 1759
不思量自难忘°
不思量自难忘° 2021-01-23 15:43

I\'m working in R.

I have a dataset in which some records contain a list of cities and counties instead of just one city or county. I\'m looking for a way to transpose

2条回答
  •  情话喂你
    2021-01-23 16:17

    d = as.data.frame(do.call(rbind, strsplit(dtaFrame$cityCountry, ",")))
    colnames(d) = c("city", "country")
    cbind(dtaFrame[,-which(colnames(dtaFrame)=="cityCountry",], d)
    

    should do it.

提交回复
热议问题