I have a data frame with, say, 5 rows, for 2 observables. I need to insert "dummy" or "zero" rows in the data frame so that number of rows per observable is the same (and can be bigger than N rows for longer one). E.g.:
# This is what I have:
x = c("a","a","b","b","b")
y = c(2,4,5,2,6)
dft = data.frame(x,y)
print(dft)
x y
1 a 2
2 a 4
3 b 5
4 b 2
5 b 6
Here's what I'd like to get, i.e. add N rows per observable to 4. Mock up df
x1 = c("a","a","a","a","b","b","b","b")
y1 = c(2,4,0,0,5,2,6,0)
dft1 = data.frame(x1,y1)
print(dft1)
x1 y1
1 a 2
2 a 4
3 a 0
4 a 0
5 b 5
6 b 2
7 b 6
8 b 0
I started with getting the N rows in original data frame per observable with ddply
, so that I know how many rows I need to add for each observable.
library(plyr)
nr = ddply(dft,.(x),summarise,val=length(x))
print(nr)
x val
1 a 2
2 b 3
# N extras will be 2 and 1 to reach 4 per obs.
repl = 4 - nr$val
repl_name = nr$x
repl_x = rep(repl_name,repl)
print(repl_x)
[1] a a b
Levels: a b
dfa = matrix("-",nrow=sum(repl),ncol=1)
dff = data.frame(repl_x,as.data.frame(dfa))
names(dff) <- names(dft)
dft = rbind(dft,dff)
dft = dft[order(as.character(dft$x)),]
print(dft)
x y
1 a 2
2 a 4
6 a -
7 a -
3 b 5
4 b 2
5 b 6
8 b -
I did achieve my goal, but in quite a few operations and transformations.
So, question - is there a simpler and faster way to insert arbitrary number of empty/dummy rows in several places in any data frame. Number of columns and rows can be any.
Note: the code above works, so I do believe this question is not a "review my code" type, but a genuine - "how to do it better" question. Thank you!
You can try using the "data.table" package which would let you use "length<-"
to expand out your rows.
Demo:
library(data.table)
as.data.table(dft)[, lapply(.SD, `length<-`, 4), by = x]
## x y z
## 1: a 2 2
## 2: a 4 3
## 3: a NA NA
## 4: a NA NA
## 5: b 5 4
## 6: b 2 5
## 7: b 6 6
## 8: b NA NA
Update
Upon provocation by Thela-the-taunter™, if you want to stick with base R, perhaps you can create a function like the following:
naRowsByGroup <- function(indf, group, rowsneeded) {
do.call(rbind, lapply(split(indf, indf[[group]]), function(x) {
x <- data.frame(lapply(x, `length<-`, rowsneeded))
x[group] <- x[[group]][1]
x
}))
}
Usage would then be:
naRowsByGroup(dft, 1, 4)
# x y z
# 1 a 2 2
# 2 a 4 3
# 3 a NA NA
# 4 a NA NA
# 5 b 5 4
# 6 b 2 5
# 7 b 6 6
# 8 b NA NA
Sample data:
x = c("a","a","b","b","b")
y = c(2,4,5,2,6)
z = c(2,3,4,5,6)
dft = data.frame(x,y,z)
dft = data.frame(x=c("a","a","b","b","b"),
y=c(2,4,5,2,6))
x <- 4 - table(dft$x)
dd <- rbind(dft, data.frame(x = rep(names(x), x), y = NA))
dd[order(dd$x), ]
# x y
# 1 a 2
# 2 a 4
# 6 a NA
# 7 a NA
# 3 b 5
# 4 b 2
# 5 b 6
# 8 b NA
And if you need, maybe you can add something if you have variables with >= 4 rows already like this
dft =data.frame(x=c("a","a","b","b","b",rep('c',6)), y=1)
x <- 4 - table(dft$x)
x[x < 0] <- 0
dd <- rbind(dft, data.frame(x = rep(names(x), x), y = NA))
dd[order(dd$x), ]
# x y
# 1 a 1
# 2 a 1
# 12 a NA
# 13 a NA
# 3 b 1
# 4 b 1
# 5 b 1
# 14 b NA
# 6 c 1
# 7 c 1
# 8 c 1
# 9 c 1
# 10 c 1
# 11 c 1
For an arbitrary number of columns:
dft = data.frame(x=c("a","a","b","b","b"),
y=c(2,4,5,2,6),
z=1,
zz=2)
x <- 4 - table(dft$x)
dd <- dft[1:sum(x), ]
dd[, names(dft)] <- NA
dd$x <- rep(names(x), x)
dd <- rbind(dft, dd)
dd[order(dd$x), ]
# x y z zz
# 1 a 2 1 2
# 2 a 4 1 2
# 6 a NA NA NA
# 7 a NA NA NA
# 3 b 5 1 2
# 4 b 2 1 2
# 5 b 6 1 2
# 8 b NA NA NA
来源:https://stackoverflow.com/questions/31175216/r-insert-multiple-rows-variable-number-in-data-frame