I'm working with panel data in R and am endeavoring to build a function that returns every user ID where PCA==1. I've largely gotten this to work, with one small problem: it only returns the values when I end the function with print() but does not do so when I end the function with return(). As I want the ids in a vector so I can later subset the data to only include those IDs, that's a problem. Code reflected below - can anyone advise on what I'm doing wrong?
The version that works (but doesn't do what I want):
retrievePCA<-function(data) {
  for (i in 1:dim(data)[1]) {
  if (data$PCA[i] == 1) {
    id<-data$CPSIDP[i]
    print(id)
  } 
  }
}
retrievePCA(data)
The version that doesn't:
retrievePCA<-function(data) {
  for (i in 1:dim(data)[1]) {
  if (data$PCA[i] == 1) {
    id<-data$CPSIDP[i]
    return(id)
  } 
  }
}
vector<-retrievePCA(data)
vector
 
    