0

enter image description here

I have a data that has 3 columns (city, score, number_of_students who get this score):

city      score       number_of_students
NYC        83              3000
NYC        94              220005
NYC        76              203
...
Chicago    92              450113 
Chicago    64              302
Chicago    98              47
Chicago    79              500021
...

Note that there are only two cities (NYC and Chicago)

How can I parse this file in R to make it look like this:

City     Score 
NYC       83
NYC       83
... (3000 rows with 'NYC 83')
NYC       94
NYC       94
...(76 rows with 'NYC 94') 

same for Chicago.

So I want in my new data table, every row is one student, and there are two columns: "city" shows whether this student is from NYC or Chicago; "Score" shows the score of this student.

How can I do this in R?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
mflowww
  • 6,835
  • 6
  • 18
  • 18

1 Answers1

2
data<-data.frame(city=c("NYC","Chicago"),score=c(83,94),number=c(5,10))
data[rep(rownames(data), data$number), ]
Soheil
  • 954
  • 7
  • 20