如何根据另一个变量行上的值添加一定数量的行
How to add certain number of rows based on values on the rows of another variable
date time td number
20150102 80000 -1 0
20150102 80001 -1 2
20150102 80002 1 0
20150102 80003 1 3
20150102 80004 -1 0
我需要根据变量 "number" 创建追加行数。并让日期和时间与编号行相同,而变量 td=0。我想要这样的数据:
date time td number
20150102 80000 -1 0
20150102 80001 -1 2
20150102 80002 1 0
20150102 80003 1 3
20150102 80004 -1 0
20150102 80001 0 NA
20150102 80001 0 NA
20150102 80003 0 NA
20150102 80003 0 NA
20150102 80003 0 NA
这个循环将使用 rbind.fill(来自 plyr),对于数据帧 df
:
for (i in length(df$n)){
x = df$n[i]
while (x > 0){
df <- rbind.fill(df, df[i,1:2])
x = x -1
print(x)
}
}
#Switch NA's in df$td column to 0
df$td[is.na(df$td)] <- 0
我会生成每一列,然后将它们绑定到一个数据框中,然后再将它们绑定到原始数据框中!无需循环。
假设您的数据框名为 df
#Create the date and time using the number column directly.
date <- rep(df$date, times = df$number)
time <- rep(df$time, times = df$number)
#Combine these fields into a data frame and set td to all 0s and number to all NAs
appenddf <- data.frame(date = date, time = time, td = 0, number = NA)
#Bind the data for appending to the original data frame
df <- rbind(df, appenddf)
另一个选项可以使用 expandRows
和 separate
函数来实现。扩展行将允许使用组合值复制 rows
,这些值稍后可以分离出来并添加到原始 df
.
library(splitstackshape)
library(dplyr)
df1 <- setDT(expandRows(df, "number"))[, newsamp :=
sprintf("%d-%d-%d-%d", date, time, 0, NA)][,newsamp] %>% as.data.frame() %>%
separate(1,c("date", "time", "td", "number"))
rbind(df, df1)
#Result
# date time td number
#1 20150102 80000 -1 0
#2 20150102 80001 -1 2
#3 20150102 80002 1 0
#4 20150102 80003 1 3
#5 20150102 80004 -1 0
#6 20150102 80001 0 NA
#7 20150102 80001 0 NA
#8 20150102 80003 0 NA
#9 20150102 80003 0 NA
#10 20150102 80003 0 NA
> a=rep(1:nrow(dat),dat$number+1)
> transform(dat[c(a[!duplicated(a)],a[duplicated(a)]),-4],num=`length<-`(dat$number,length(a)))
date time td num
1 20150102 80000 -1 0
2 20150102 80001 -1 2
3 20150102 80002 1 0
4 20150102 80003 1 3
5 20150102 80004 -1 0
2.1 20150102 80001 -1 NA
2.2 20150102 80001 -1 NA
4.1 20150102 80003 1 NA
4.2 20150102 80003 1 NA
4.3 20150102 80003 1 NA
date time td number
20150102 80000 -1 0
20150102 80001 -1 2
20150102 80002 1 0
20150102 80003 1 3
20150102 80004 -1 0
我需要根据变量 "number" 创建追加行数。并让日期和时间与编号行相同,而变量 td=0。我想要这样的数据:
date time td number
20150102 80000 -1 0
20150102 80001 -1 2
20150102 80002 1 0
20150102 80003 1 3
20150102 80004 -1 0
20150102 80001 0 NA
20150102 80001 0 NA
20150102 80003 0 NA
20150102 80003 0 NA
20150102 80003 0 NA
这个循环将使用 rbind.fill(来自 plyr),对于数据帧 df
:
for (i in length(df$n)){
x = df$n[i]
while (x > 0){
df <- rbind.fill(df, df[i,1:2])
x = x -1
print(x)
}
}
#Switch NA's in df$td column to 0
df$td[is.na(df$td)] <- 0
我会生成每一列,然后将它们绑定到一个数据框中,然后再将它们绑定到原始数据框中!无需循环。
假设您的数据框名为 df
#Create the date and time using the number column directly.
date <- rep(df$date, times = df$number)
time <- rep(df$time, times = df$number)
#Combine these fields into a data frame and set td to all 0s and number to all NAs
appenddf <- data.frame(date = date, time = time, td = 0, number = NA)
#Bind the data for appending to the original data frame
df <- rbind(df, appenddf)
另一个选项可以使用 expandRows
和 separate
函数来实现。扩展行将允许使用组合值复制 rows
,这些值稍后可以分离出来并添加到原始 df
.
library(splitstackshape)
library(dplyr)
df1 <- setDT(expandRows(df, "number"))[, newsamp :=
sprintf("%d-%d-%d-%d", date, time, 0, NA)][,newsamp] %>% as.data.frame() %>%
separate(1,c("date", "time", "td", "number"))
rbind(df, df1)
#Result
# date time td number
#1 20150102 80000 -1 0
#2 20150102 80001 -1 2
#3 20150102 80002 1 0
#4 20150102 80003 1 3
#5 20150102 80004 -1 0
#6 20150102 80001 0 NA
#7 20150102 80001 0 NA
#8 20150102 80003 0 NA
#9 20150102 80003 0 NA
#10 20150102 80003 0 NA
> a=rep(1:nrow(dat),dat$number+1)
> transform(dat[c(a[!duplicated(a)],a[duplicated(a)]),-4],num=`length<-`(dat$number,length(a)))
date time td num
1 20150102 80000 -1 0
2 20150102 80001 -1 2
3 20150102 80002 1 0
4 20150102 80003 1 3
5 20150102 80004 -1 0
2.1 20150102 80001 -1 NA
2.2 20150102 80001 -1 NA
4.1 20150102 80003 1 NA
4.2 20150102 80003 1 NA
4.3 20150102 80003 1 NA