r - creating id for each observation in panel data -
i have data set person names think id misrepresented (the ids not unique per person right in data set). create new id vector, wondering how. instance if have following list of names:
name john john john tom tom david david sonia sonia ben
how can create unique id per person?
id name 1 john 1 john 1 john 2 tom 2 tom 3 david 3 david 4 sonia 4 sonia 5 ben
seems simple process, i'm not sure how can use loop! suggestions help.
i came loop looks close error:
> (i in 1:5){ + a<-unique(dat$name) + a<-as.vector(a) + dat$id[a[i]]<-i + dat$id[a[i+1]]<-i+1 + } error in `$<-.data.frame`(`*tmp*`, "id", value = c(5l, 5l, 5l, 5l, 5l, : replacement has 11 rows, data has 10
use , may
dat$id <- as.numeric(as.factor(dat$name))
where "id" new column , "name" name column, dat original data frame
Comments
Post a Comment