Jim G.
Jim G.

Reputation: 15363

How can I take a percentage-wise subset of a data frame?

Consider the following code:

baseball <- read.csv("c:\\Users\\Jim\\Downloads\\MLB2008.csv", header = T)  
baseball[1:70,]

This code will print out the first 70 rows of the data frame named 'baseball'.

How can I do this?

Upvotes: 1

Views: 2533

Answers (2)

mpalanco
mpalanco

Reputation: 13570

Using head and tail.

head(baseball, round(nrow(baseball)*0.7))
tail(baseball, round(nrow(baseball)*0.3))

Upvotes: 4

bramtayl
bramtayl

Reputation: 4024

cutoff = round(0.7*nrow(baseball))

baseball[1:cutoff,]
baseball[-(1:cutoff),]

Upvotes: 3

Related Questions