Assign names based on rows to repeat in r

35 views Asked by At

I have two data frames Name_List and Fruits_List. I would like to produce the dataframe Output. Output in such a way that Name should repeat based on the values in Rows_to_repeat column. Please let me know if anyone has the solution.

Name=c("rohit","murali","partha")
Rows_to_repeat=c(6,3,1)

Fruits=c("Apple","Orange","Watermelon","Mango","Banana","Kiwi","Pomo","Dates","Muskmelon","Papaya")
Person_Got_Fruit=c("rohit","rohit","rohit","rohit","rohit","rohit","murali","murali","murali","partha")

Name_List=data.frame(Name,Rows_to_repeat)
Fruits_List=data.frame(Fruits)

Output=data.frame(Fruits,Person_Got_Fruit)
1

There are 1 answers

0
ParthaSarathi On

This is the Solution which is working for me

Person_Got_Fruit=data.frame(rep(Name_List$Name, Name_List$Rows_to_repeat))

Output=cbind(Fruits_List,Person_Got_Fruit)