Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何使用特定模式重复数据帧的行?_R_Dataframe - Fatal编程技术网

R 如何使用特定模式重复数据帧的行?

R 如何使用特定模式重复数据帧的行?,r,dataframe,R,Dataframe,我有一个像这样的数据框 col1 col2 col3 col4 col5 1 A 12 13 14 2 B 87 56 44 3 C 45 23 33 4 D 56 87 56 和一个向量 c("E", "F", "G") 我必须得到这样的东西: col1 col2 col

我有一个像这样的数据框

col1    col2    col3    col4    col5
1        A       12      13      14
2        B       87      56      44
3        C       45      23      33
4        D       56      87      56
和一个向量

 c("E", "F", "G")
我必须得到这样的东西:

col1    col2    col3    col4    col5
1        A       12      13      14
0        E       0       0       0
0        F       0       0       0
0        G       0       0       0
2        B       87      56      44
0        E       0       0       0
0        F       0       0       0
0        G       0       0       0
3        C       45      23      33
0        E       0       0       0
0        F       0       0       0
0        G       0       0       0
4        D       56      87      56
0        E       0       0       0
0        F       0       0       0
0        G       0       0       0
我可以用for循环得到这个数据帧,但那会很乏味。有没有简洁的方法来实现这一点


提前谢谢

添加新行很简单,但是按照您想要的方式对它们进行排序很棘手。如果最终顺序不重要,您可以忽略排序.vec:

txt = "col1    col2    col3    col4    col5
1        A       12      13      14
2        B       87      56      44
3        C       45      23      33
4        D       56      87      56"

tmp <- read.table(text=txt, header=T, stringsAsFactors=F)
v2 <- c("E", "F", "G")

# add the mostly empty data
tmp2 <- data.frame(col1=0, col2=rep(v2, nrow(tmp)),
                   col3=0, col4=0, col5=0)

# encoding vector for sorting at the end (took some figuring out)
sorting.vec <- 1000* c(1:nrow(tmp), rep(1:nrow(tmp), each=length(v2))) +
               c(rep(0, nrow(tmp)), rep(1:length(v2), nrow(tmp)))

# stack and sort apropriately
final <- rbind(tmp, tmp2)[order(sorting.vec),]
txt=“col1 col2 col3 col4 col5
1 A 12 13 14
2B875644
3 C 45 23 33
4 D 56 87 56“

tmp您可以尝试使用
Map
生成序列

df <- data.frame(col1 = 1:4,
col2 = LETTERS[1:4],
col3 = c(12,87,45,56),
col4=c(13,56,23,87),
col5=c(14,44,33,56))

vec <- c("E","F","G")

df2 <- data.frame(col1 = 0, col2 = vec, col3=0, col4=0, col5=0)

#rbind
df3 <- rbind(df2, df)

#generate new sequence for rows
df4<-df3[unlist(Map(c, (nrow(df2)+1):nrow(df3), list(1:nrow(df2)))),]
df这里有一个使用行索引的可能(矢量化)解决方案。假设您的数据名为
df
,向量名为
V
,可以尝试

df2 <- df[rep(seq_len(nrow(df)), each = length(V) + 1), ]
df2[grep("[.]", row.names(df2)), ] <- 0
df2[grep("[.]", row.names(df2)), "col2"] <- V
# row.names(df2) <- seq_len(nrow(df2)) # Optional, if you don't like your row names
df2
#    col1 col2 col3 col4 col5
# 1     1    A   12   13   14
# 2     0    E    0    0    0
# 3     0    F    0    0    0
# 4     0    G    0    0    0
# 5     2    B   87   56   44
# 6     0    E    0    0    0
# 7     0    F    0    0    0
# 8     0    G    0    0    0
# 9     3    C   45   23   33
# 10    0    E    0    0    0
# 11    0    F    0    0    0
# 12    0    G    0    0    0
# 13    4    D   56   87   56
# 14    0    E    0    0    0
# 15    0    F    0    0    0
# 16    0    G    0    0    0

df2您可以创建所需表的结构,然后简单地从原始数据帧分配行

df <- read.table(header=T, text='
                 col1    col2    col3    col4    col5
1        A       12      13      14
2        B       87      56      44
3        C       45      23      33
4        D       56      87      56')

v1 <- c(levels(df$col2))
v2 <- c(LETTERS[5:7])
new_df <- do.call("rbind", sapply(1:length(v1), FUN=function(x) expand.grid(0,c(v1[x],v2),0,0,0), simplify=F))

new_df[seq(from=1,to=nrow(new_df), by=4),] <- df
colnames(new_df) <- colnames(df)

   col1 col2 col3 col4 col5
1     1    A   12   13   14
2     0    E    0    0    0
3     0    F    0    0    0
4     0    G    0    0    0
5     2    B   87   56   44
6     0    E    0    0    0
7     0    F    0    0    0
8     0    G    0    0    0
9     3    C   45   23   33
10    0    E    0    0    0
11    0    F    0    0    0
12    0    G    0    0    0
13    4    D   56   87   56
14    0    E    0    0    0
15    0    F    0    0    0
16    0    G    0    0    0

df一个旁注,需要打开stringsAsFactors=F,否则可能会有警告信息:在
[@KFB,是的,你是对的。我已经自动完成了这个,所以完全忘记了它