GKyle
GKyle

Reputation: 679

How to paste row names in a for loop with R

I'm trying to paste row names in a for loop in R. This is so I can keep the id in the output for each loop for easier segmentation.

My for loop works well. It's just that my paste function does not.

UK_profileList is data frame. The function get_ga below is from the RGA package.

My code is:

start.date <- "2015-01-01"
end.date <- format(Sys.Date(), format = "%Y-%m-%d")

for (i in 1:nrow(UK_profileList)) {

  allData <- get_ga(UK_profileList[i,], start.date, end.date, dimensions = "ga:month, ga:day, ga:minute", metrics = "ga:sessions")

  row.names(allData[i]) <- paste(UK_profileList[i,], sep="")

}

I get the error:

Error in

row.names<-.data.frame(tmp`, value = "8131437") : invalid 'row.names length

Please note, I am aware of this post here:using paste() in a for loop with glm

But it is specific to glm...

Upvotes: 0

Views: 5510

Answers (1)

Andrelrms
Andrelrms

Reputation: 819

This is what I was talking about in the comment:

allData<-data.frame(a=rnorm(10),b=rexp(10))
new.names<-NULL
for(i in 1:10){
 new.names[i]<-paste(letters[i],i,sep="")
}

row.names(allData)<-new.names

Upvotes: 1

Related Questions