2017-05-29 88 views
0

这里我data.frame的示例创建重复展开data.frame:通过基于群组条件

df = read.table(text = 'ID Day Count Count_group 
1001 1933 6 11 
1002 1933 6 11 
1003 1933 6 11 
1004 1933 6 11 
1005 1933 6 11 
1006 1933 6 11 
1007 1932 5 8 
1008 1932 5 8 
1009 1932 5 8 
1010 1932 5 8 
1011 1932 5 8 
1012 1931 3 4 
1013 1931 3 4 
1014 1931 3 4 
1015 1930 1 1 
1016 1800 6 10 
1017 1800 6 10 
1018 1800 6 10 
1019 1800 6 10 
1020 1800 6 10 
1021 1800 6 10 
1022 1799 4 6 
1023 1799 4 6 
1024 1799 4 6 
1025 1799 4 6 
1026 1798 2 2 
1027 1798 2 2 
1028 888  4 6 
1029 888  4 6 
1030 888  4 6 
1031 888  4 6 
1032 887  2 3 
1033 887  2 3 
1034 886  1 2 
1035 885  1 1', header = TRUE) 

Count COL显示每每个DayID值和Count_group栏的总数示出的总和的ID值每个DayDay - 1

例如1933 = Count_group 11因为Count 6(1933)+ Count 5(1932)等等。

我需要做的是创造每每个Count_group重复的意见,并把它们添加到它,以每各Count_groupDayDay - 1显现。

例如需要Count_group = 11通过的Day 1933和1932因此,无论天Count值由要被包括在所述Count_group = 11 下一个将是Count_group = 8,由1932和1931组成,等...

预期输出:

ID  Day Count Count_group 
1001 1933 6 11 
1002 1933 6 11 
1003 1933 6 11 
1004 1933 6 11 
1005 1933 6 11 
1006 1933 6 11 
1007 1932 5 11 
1008 1932 5 11 
1009 1932 5 11 
1010 1932 5 11 
1011 1932 5 11 
1007 1932 5 8 
1008 1932 5 8 
1009 1932 5 8 
1010 1932 5 8 
1011 1932 5 8 
1012 1931 3 8 
1013 1931 3 8 
1014 1931 3 8 
1012 1931 3 4 
1013 1931 3 4 
1014 1931 3 4 
1015 1930 1 4 
1015 1930 1 1 
1016 1800 6 10 
1017 1800 6 10 
1018 1800 6 10 
1019 1800 6 10 
1020 1800 6 10 
1021 1800 6 10 
1022 1799 4 10 
1023 1799 4 10 
1024 1799 4 10 
1025 1799 4 10 
1022 1799 4 6 
1023 1799 4 6 
1024 1799 4 6 
1025 1799 4 6 
1026 1798 2 6 
1027 1798 2 6 
1026 1798 2 2 
1027 1798 2 2 
1028 888 4 6 
1029 888 4 6 
1030 888 4 6 
1031 888 4 6 
1032 887 2 6 
1033 887 2 6 
1032 887 2 3 
1033 887 2 3 
1034 886 1 3 
1034 886 1 2 
1035 885 1 2 
1035 885 1 1 

你有什么建议吗?

+0

好吧,我会添加更多的信息。谢谢 – aaaaa

+0

@akrun info已被添加。谢谢 – aaaaa

+0

谢谢,与其他的东西忙碌:-)这意味着'天'不是一个分组变量 – akrun

回答

1

我认为这确实你需要什么...

#first add a grouping variable 
df$daygroup <- c(0,cumsum(sapply(2:nrow(df),function(i) df$Day[i]!=df$Day[i-1]))) 

#split df into a list of data frames, loop through them to add extra rows, 
#and bind them back together 
df2 <- do.call(rbind,lapply(split(df,df$daygroup),function(x){ 
    n <- nrow(x) 
    m <- x$Count_group[1] #number of rows needed for Day 
    if(m>n){ 
    y <- rbind(x,data.frame(ID=(x$ID[n]+1):(x$ID[n]+m-n), #continue numbering 
          Day=x$Day[1]-1, #previous day 
          Count=m-x$Count[1], #difference in count 
          Count_group=m, 
          daygroup=x$daygroup[1])) 
    } else { 
    y <- x #no extra rows needed 
    } 
    return(y) 
} 
)) 
df2$daygroup <- NULL #remove grouping variable 


head(df2,20) #ignore the rownames! 
     ID Day Count Count_group 
0.1 1001 1933  6   11 
0.2 1002 1933  6   11 
0.3 1003 1933  6   11 
0.4 1004 1933  6   11 
0.5 1005 1933  6   11 
0.6 1006 1933  6   11 
0.7 1007 1932  5   11 
0.8 1008 1932  5   11 
0.9 1009 1932  5   11 
0.10 1010 1932  5   11 
0.11 1011 1932  5   11 
1.7 1007 1932  5   8 
1.8 1008 1932  5   8 
1.9 1009 1932  5   8 
1.10 1010 1932  5   8 
1.11 1011 1932  5   8 
1.1 1012 1931  3   8 
1.2 1013 1931  3   8 
1.3 1014 1931  3   8 
2.12 1012 1931  3   4 
+0

嗨,非常感谢。我更新了这个问题,你应该很容易调整代码。 1)如果我的身份证号码不是按顺序排列,并且可以在不同的日期内重复,该怎么办? 2)如何将代码适应不同的数据框架,将不同的时间窗口视为分组条件?即我应该更改哪部分代码以及如何更改。谢谢 – aaaaa

+0

新问题在这里https://stackoverflow.com/questions/44242299/expand-data-frame-by-creating-duplicates-based-on-group-condition-2谢谢.. – aaaaa

+1

我已经添加了一个答案给你关于第一部分关于ID值的新问题。对于较长时间,我不清楚正确的输出是什么样子。 –