如何在更改 1 或 2 个变量的同时重复数据框的最后一行 n 次?
How do I repeat the last row of a data frame n times, while changing 1 or 2 variables?
我有一个队列预期寿命数据,我想将最后一行重复 n 次,但要更改一些值。我想找到一个可以应用于所有大小数据框的通用函数。
> df <- data.frame(Year = c(2000,2001,2002), Age = c(0,1,2), x = c(1,2,3), y = c(0.3,0.7,0.5))
> df
Year Age x y
1 2000 0 1 0.3
2 2001 1 2 0.7
3 2002 2 3 0.5
我想重复最后一行,比如 3 次,同时为我创建的每个新行将 Year 和 Age 的值增加 1,如下所示:
> df2
Year Age x y
1 2000 0 1 0.3
2 2001 1 2 0.7
3 2002 2 3 0.5
4 2003 3 3 0.5
5 2004 4 3 0.5
6 2005 5 3 0.5
基本上增加 Year 和 Age 的值,但让 x 和 y 保持不变。
有点不清楚这里的用例是什么,所以很难给你一个可靠的解决方案,但一个快速的方法是:
# your initial dataframe
df <- data.frame(Year = c(2000,2001,2002), Age = c(0,1,2), x = c(1,2,3), y = c(0.3,0.7,0.5))
# set the number you'd like to replicate
n <- 5
# create another df with similar columns (this is unnecessary as you could've done it from the beginning)
df2 <- data.frame(Year = c(2003:(2003+n)), Age = c(3:(3+n)), x = rep(3, n), y = rep(0.5, n))
# then bind the frames
final_df <- rbind(df, df2)
这有帮助吗?
-布伦南
你可以重复最后一个行号n次,并且在Age上加上seq(n)
使其增加1,即
rbind(df, transform(df[rep(nrow(df), 3),], Age = Age + seq(3), Year = Year + seq(3)))
# Year Age x y
#1 2000 0 1 0.3
#2 2001 1 2 0.7
#3 2002 2 3 0.5
#31 2003 3 3 0.5
#3.1 2004 4 3 0.5
#3.2 2005 5 3 0.5
这里发布的其他好方法略有不同:
df[4:6, ] <- df[3, ]
# make new rows numbered 4 to 6 as copies of row 3
df$Year[4:6] <- 2003:2005
# overwrite new parts of Year variable
df$Age[4:6] <- 3:5
# overwrite new parts of Age variable
@Sotos解决方案的dplyr
方法:
df %>%
bind_rows(df[rep(nrow(df), 3),] %>%
mutate(Age = Age + seq(3),
Year = Year + seq(3)))
我有一个队列预期寿命数据,我想将最后一行重复 n 次,但要更改一些值。我想找到一个可以应用于所有大小数据框的通用函数。
> df <- data.frame(Year = c(2000,2001,2002), Age = c(0,1,2), x = c(1,2,3), y = c(0.3,0.7,0.5))
> df
Year Age x y
1 2000 0 1 0.3
2 2001 1 2 0.7
3 2002 2 3 0.5
我想重复最后一行,比如 3 次,同时为我创建的每个新行将 Year 和 Age 的值增加 1,如下所示:
> df2
Year Age x y
1 2000 0 1 0.3
2 2001 1 2 0.7
3 2002 2 3 0.5
4 2003 3 3 0.5
5 2004 4 3 0.5
6 2005 5 3 0.5
基本上增加 Year 和 Age 的值,但让 x 和 y 保持不变。
有点不清楚这里的用例是什么,所以很难给你一个可靠的解决方案,但一个快速的方法是:
# your initial dataframe
df <- data.frame(Year = c(2000,2001,2002), Age = c(0,1,2), x = c(1,2,3), y = c(0.3,0.7,0.5))
# set the number you'd like to replicate
n <- 5
# create another df with similar columns (this is unnecessary as you could've done it from the beginning)
df2 <- data.frame(Year = c(2003:(2003+n)), Age = c(3:(3+n)), x = rep(3, n), y = rep(0.5, n))
# then bind the frames
final_df <- rbind(df, df2)
这有帮助吗?
-布伦南
你可以重复最后一个行号n次,并且在Age上加上seq(n)
使其增加1,即
rbind(df, transform(df[rep(nrow(df), 3),], Age = Age + seq(3), Year = Year + seq(3)))
# Year Age x y
#1 2000 0 1 0.3
#2 2001 1 2 0.7
#3 2002 2 3 0.5
#31 2003 3 3 0.5
#3.1 2004 4 3 0.5
#3.2 2005 5 3 0.5
这里发布的其他好方法略有不同:
df[4:6, ] <- df[3, ]
# make new rows numbered 4 to 6 as copies of row 3
df$Year[4:6] <- 2003:2005
# overwrite new parts of Year variable
df$Age[4:6] <- 3:5
# overwrite new parts of Age variable
@Sotos解决方案的dplyr
方法:
df %>%
bind_rows(df[rep(nrow(df), 3),] %>%
mutate(Age = Age + seq(3),
Year = Year + seq(3)))