我有一个如下所示的数据框:
STAND Plot1 X DBH Spec HT NOTES
1 TEN YEAR VAR RET 1 s 1.25 ASH NA
2 TEN YEAR VAR RET 1 s 1.25 ASH NA
3 TEN YEAR VAR RET 1 s 1.25 ASH NA
4 TEN YEAR VAR RET 1 s 1.25 ASH NA
5 TEN YEAR VAR RET 1 s 1.25 ASH NA
我需要多次重复一行行(不同块的不同数量)。所以我在每个展台都有一个展台(STAND)和5 * 2地块。 5个图是小的(X =“s”),另外5个是大的(X =“L”)。我需要为每个大图重复20次行,每个小图重复500次。所以重复块的条件是这样的:
STAND == "TEN YEAR VAR RET", Plot1 == "1", X == "s" * 500
STAND == "TEN YEAR VAR RET", Plot1 == "2", X == "s" * 500 etc
对于大型情节,如果是这样的话:
STAND == "TEN YEAR VAR RET", Plot1 == "1", X == "L" * 20
STAND == "TEN YEAR VAR RET", Plot1 == "2", X == "L" * 20 etc
任何建议表示赞赏!
答案 0 :(得分:1)
我们可以创建一些行的数字索引
#row index with 's' value for X
i1 <- with(df1, which(STAND == "TEN YEAR VAR RET" & Plot1 %in% c("1", "2")& X == "s"))
#row index for 'L' value for X
i2 <- with(df1, which(STAND == "TEN YEAR VAR RET" & Plot1 %in% c("1", "2")& X == "L"))
#row index for those that doesn't belong to the above 2
i3 <- which(!seq_len(nrow(df1)) %in% c(i1, i2))
然后复制行以展开数据集
n1 <- 500
n2 <- 20
df2 <- df1[sort(c(rep(i1, each = n1), i3, rep(i2, each = n2))),]
答案 1 :(得分:1)
数据强>
df <- read.table(text="
STAND Plot1 X DBH Spec HT
'TEN YEAR VAR RET' 1 s 1.25 ASH NA
'TEN YEAR VAR RET' 2 s 1.25 ASH NA
'TEN YEAR VAR RET' 2 L 1.25 ASH NA
'TEN YEAR VAR RET' 1 s 1.25 ASH NA
'TEN YEAR VAR RET' 3 L 1.25 ASH NA
'AAAAAAAAAAAAAAAA' 2 s 1.25 ASH NA
'AAAAAAAAAAAAAAAA' 3 s 1.25 ASH NA
'TEN YEAR VAR RET' 2 s 1.25 ASH NA",stringsAsFactors=F,header=T)
<强>解决方案强>
library(dplyr)
library(tidyr)
library(magrittr)
df %>%
mutate(times=case_when(
Plot1 == 1 ~ 2, # <- change values (and possibly condition) here
Plot1 == 2 ~ 3, # <- and here
TRUE ~ 1)) %>%
group_by(times) %>%
nest %$%
map2_dfr(data,times,~.x[rep(seq(nrow(.x)),each=.y),])
# A tibble: 18 x 6
# STAND Plot1 X DBH Spec HT
# <chr> <int> <chr> <dbl> <chr> <lgl>
# 1 TEN YEAR VAR RET 1 s 1.25 ASH NA
# 2 TEN YEAR VAR RET 1 s 1.25 ASH NA
# 3 TEN YEAR VAR RET 1 s 1.25 ASH NA
# 4 TEN YEAR VAR RET 1 s 1.25 ASH NA
# 5 TEN YEAR VAR RET 2 s 1.25 ASH NA
# 6 TEN YEAR VAR RET 2 s 1.25 ASH NA
# 7 TEN YEAR VAR RET 2 s 1.25 ASH NA
# 8 TEN YEAR VAR RET 2 L 1.25 ASH NA
# 9 TEN YEAR VAR RET 2 L 1.25 ASH NA
# 10 TEN YEAR VAR RET 2 L 1.25 ASH NA
# 11 AAAAAAAAAAAAAAAA 2 s 1.25 ASH NA
# 12 AAAAAAAAAAAAAAAA 2 s 1.25 ASH NA
# 13 AAAAAAAAAAAAAAAA 2 s 1.25 ASH NA
# 14 TEN YEAR VAR RET 2 s 1.25 ASH NA
# 15 TEN YEAR VAR RET 2 s 1.25 ASH NA
# 16 TEN YEAR VAR RET 2 s 1.25 ASH NA
# 17 TEN YEAR VAR RET 3 L 1.25 ASH NA
# 18 AAAAAAAAAAAAAAAA 3 s 1.25 ASH NA