Creating Sets of Samples From Given dataframe using condition R

懵懂的女人 提交于 2019-12-11 19:51:26

问题


I have input table having more than 750 K raws. It has a field called quarter. I want to create sample such that I get 10% records from each quarter. Main attributes of the data.frame are:

  1. "SERIAL_NBR"
  2. "MODELNO"
  3. "War.Start.Monthly"

"Start.Qua.Yr" is the field where quarter is mentioned. Is there any way through which I can generate sample data which has data(10% of record) for each quarter?

Using sample function I can get sample regardless of the quarter. Code for the same will be:

raw_claim_input[sample(1:nrow(raw_claim_input),as.integer(nrow(raw_claim_input)/10)),]

When I am doing following for one quarter I am not getting expected results as there a logical problem while considering values

raw_claim_input[sample(1:nrow(raw_claim_input[raw_claim_input$War.Start.Monthly=="08-M2",]),as.integer(nrow(raw_claim_input[raw_claim_input$War.Start.Monthly=="08-M2",])/10)),]

The value 08-M2 is the filter, I want to do it for all the values available. There are 70 values for War.Start.Monthly, and I want to generate sample for each value of War.Start.Monthly.

Part of data

     Day.Covered           SHIP_DATE Warranty.Start.Qua.Yr War.Start.Monthly AssemblyDateUpdated Warranty.End.Date Warranty.End.Qur.Yr War.End.Monthly
252754         365    06-04-2008 00:00                 08-Q2             08-M6    06-03-2008 00:00        08-04-2064               64-Q2           64-M4
441605        1095 08-17-2010 11:13:07                 10-Q3             10-M8 08-16-2010 12:09:57        08-04-2064               64-Q2           64-M4
583636         731 10-17-2012 00:00:00                 12-Q4            12-M10 10-16-2012 00:00:00        08-04-2064               64-Q2           64-M4
115586         731    01-04-2013 00:00                 13-Q1             13-M1    01-03-2013 00:00        08-04-2064               64-Q2           64-M4
334221        1095 06-13-2011 12:29:23                 11-Q2             11-M6    06-11-2011 11:25        08-04-2064               64-Q2           64-M4
146656        1095 03-16-2011 10:54:37                 11-Q1             11-M3 03-15-2011 08:14:40        08-04-2064               64-Q2           64-M4
249956        1095 06-18-2008 12:35:06                 08-Q2             08-M6    06-06-2008 10:51        08-04-2064               64-Q2           64-M4
276295         731 05-18-2011 00:00:00                 11-Q2             11-M5 05-18-2011 00:00:00        19-11-2014               14-Q4          14-M11
582423         731 10-22-2012 00:00:00                 12-Q4            12-M10 10-22-2012 00:00:00        08-04-2064               64-Q2           64-M4
380369         730    08-04-2009 17:43                 09-Q3             09-M7 07-31-2009 07:14:17        18-01-2012               12-Q1           12-M1

Please let me know if more details needed.


回答1:


This will do:

X <- read.csv(text="Day.Covered,SHIP_DATE,Warranty.Start.Qua.Yr,War.Start.Monthly,AssemblyDateUpdated,Warranty.End.Date,Warranty.End.Qur.Yr,War.End.Monthly
 365,    06-04-2008 00:00, 08-Q2,  08-M6,    06-03-2008 00:00, 08-04-2064 ,64-Q2,  64-M4
1095, 08-17-2010 11:13:07, 10-Q3,  10-M8, 08-16-2010 12:09:57, 08-04-2064 ,64-Q2,  64-M4
 731, 10-17-2012 00:00:00, 12-Q4, 12-M10, 10-16-2012 00:00:00, 08-04-2064 ,64-Q2,  64-M4
 731,    01-04-2013 00:00, 13-Q1,  13-M1,    01-03-2013 00:00, 08-04-2064 ,64-Q2,  64-M4
1095, 06-13-2011 12:29:23, 11-Q2,  11-M6,    06-11-2011 11:25, 08-04-2064 ,64-Q2,  64-M4
1095, 03-16-2011 10:54:37, 11-Q1,  11-M3, 03-15-2011 08:14:40, 08-04-2064 ,64-Q2,  64-M4
1095, 06-18-2008 12:35:06, 08-Q2,  08-M6,    06-06-2008 10:51, 08-04-2064 ,64-Q2,  64-M4
 731, 05-18-2011 00:00:00, 11-Q2,  11-M5, 05-18-2011 00:00:00, 19-11-2014 ,14-Q4, 14-M11
 731, 10-22-2012 00:00:00, 12-Q4, 12-M10, 10-22-2012 00:00:00, 08-04-2064 ,64-Q2,  64-M4
 730,    08-04-2009 17:43, 09-Q3,  09-M7, 07-31-2009 07:14:17, 18-01-2012 ,12-Q1,  12-M")

# Replicate X to have enough data for this example.
X <- X[rep(seq(nrow(X)), 100),]

# Partition the data according to quarter.
partitions <- split(X, X$Warranty.Start.Qua.Yr)
# Draw samples from each partition.
samples <- lapply(partitions, function(p) p[sample(nrow(p), nrow(p)/10),])


来源:https://stackoverflow.com/questions/19657679/creating-sets-of-samples-from-given-dataframe-using-condition-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!