Justine943
Justine943

Reputation: 9

split in R and 'for' loop

I have a small problem with loop 'for' in R.

I'm splitting the data regarding one of columns. I want to apply 'for' loop for splitted file.

How can I do that?

input_data <- read.csv2("C:/Users/justyna.andrulewicz/Desktop/R estimator/data2.csv", sep=",")
data <- as.data.frame(input_data)
z <-data$Id
zz <-split(data, z, drop = FALSE)

I can define as an object one particular group like that:

zz_1<-zz[[1]]

but it's not enough for me :D because in my problem I don't know how many groups it will be in splitted file.

Please give any suggestion to me .

Upvotes: 0

Views: 3049

Answers (1)

minem
minem

Reputation: 3650

In that case you can create indexes like this

for (i in 1: length(z)){
z[[i]]
}

Edit:

as Konrad Rudolph suggest:

Better yet, of course, is to iterate over the object directly. In this case, just do

 for (i in z){
print(i) # or do whatever you want    
}

Upvotes: 1

Related Questions