Tomas
Tomas

Reputation: 115

Trying to store every fifth record in the Iris dataset in R

Using the Iris data set in R, I'm trying to store every fifth record in a “test” dataset starting with the first record.

View(iris)
test<-iris[-idx,5]

This just showed me what's in the fifth column of the dataset. I'm looking to show every fifth row starting with the first. How exactly do I go about this? Thanks

Upvotes: 1

Views: 197

Answers (2)

lmo
lmo

Reputation: 38500

Here is a method using modulus (%%):

test <- iris[seq.int(nrow(iris)) %% 5) == 1, ]

Upvotes: 3

akrun
akrun

Reputation: 887148

We can use seq

test <- iris[seq(1, nrow(iris), by = 5),]

Or using a logical vector to recycle to the end of the rows

test <- iris[c(TRUE, rep(FALSE, 4)),]

Upvotes: 3

Related Questions