Creating a new column using for/nested loop in r

Viewed 236

Just getting started using R and I need some help in understanding the application of for/nested loop.

StudyID<-c(1:5)
SubjectID<-c(1:5)

df<-data.frame(StudyID=rep(StudyID, each=5), SubjectID=rep(SubjectID, each=1))

How can I create a new column called as ID, which would use the combination of studyID and subjectID to create a unique ID ?

So for this data, unique ID should be from 1:25.

So the final data looks like this:

UniqueID<- c(1:25)

df<-cbind(df,UniqueID)

View(df)

Is there any other way which is faster and more efficient that looping ?

3 Answers
Related