R 是否有方法编辑此函数以将数字添加到同一列?

R 是否有方法编辑此函数以将数字添加到同一列?,r,dataframe,integer,R,Dataframe,Integer,我有一个函数,可以将整数添加到现有的数据帧中。它接受我的整数,并将它们作为列转置到数据帧中。当我添加值较短的列时,行将自动指定为0。当我添加另一个长度较长的列时,所有长度较短的现有列都会添加0,使其与最长列的长度相同。例如 作用 add_peaks <- function(df, vec, colname) { colname <- title new_row <- max(nrow(df), length(vec)) new_df <- df[1:new_r

我有一个函数,可以将整数添加到现有的数据帧中。它接受我的整数,并将它们作为列转置到数据帧中。当我添加值较短的列时,行将自动指定为0。当我添加另一个长度较长的列时,所有长度较短的现有列都会添加0,使其与最长列的长度相同。例如

作用

add_peaks <- function(df, vec, colname) {
  colname <- title
  new_row <- max(nrow(df), length(vec))
  new_df <- df[1:new_row, ,drop = FALSE]
  new_df[colname] <- c(vec, rep(NA, new_row - length(vec)))
  new_df[is.na(new_df)] <- 0
  rownames(new_df) <- NULL
  new_df
}
整数

Bpeaks = 2 1 6 12 10 5 8
Cpeaks = 2 1
Dpeaks = 4 1 0 9 20 4 18 11 9 6
更新的数据帧

Apeaks
1
6
5
Apeaks Bpeaks Cpeaks Dpeaks
1      2      2      4
6      1      1      1
5      6      0      0
0      12     0      9
0      10     0      20
0      5      0      4
0      8      0      18
0      0      0      11
0      0      0      9
0      0      0      6

是否有方法编辑函数,将相同名称的列(例如“Dpeaks”)添加到已存在的“Dpeaks”列中,同时将0添加到长度较短的其余列中?例如…

Dpeaks = 2 1 4

Apeaks Bpeaks Cpeaks Dpeaks
1      2      2      4
6      1      1      1
5      6      0      0
0      12     0      9
0      10     0      20
0      5      0      4
0      8      0      18
0      0      0      11
0      0      0      9
0      0      0      6
0      0      0      2
0      0      0      1
0      0      0      4

谢谢你的帮助

如果我们可以从
rowr

library(rowr)
cbind.fill(df1, Bpeaks, Cpeaks, Dpeaks, fill = 0)

或者使用
base R
,将向量保留在
列表中

lst1 <- list(Apeaks = df1$Apeaks, Bpeaks = Bpeaks, 
         Cpeaks = Cpeaks, Dpeaks = Dpeaks)
mx <- max(lengths(lst1))
out <-sapply(lst1, `length<-`, mx)
out[is.na(out)] <- 0
out
#      Apeaks Bpeaks Cpeaks Dpeaks
# [1,]      1      2      2      4
# [2,]      6      1      1      1
# [3,]      5      6      0      0
# [4,]      0     12      0      9
# [5,]      0     10      0     20
# [6,]      0      5      0      4
# [7,]      0      8      0     18
# [8,]      0      0      0     11
# [9,]      0      0      0      9
#[10,]      0      0      0      6

lst1如果列已经存在,则可以附加向量:

add_peaks <- function(df, vec, colname) {
   if(colname %in% names(df)) vec <- c(df[[colname]], vec)
   new_row <- max(nrow(df), length(vec))
   new_df <- df[1:new_row, ,drop = FALSE]
   new_df[colname] <- c(vec, rep(NA, new_row - length(vec)))
   new_df[is.na(new_df)] <- 0
   rownames(new_df) <- NULL
   new_df
}

add\u谢谢!你又是一个救命恩人。
add_peaks <- function(df, vec, colname) {
   if(colname %in% names(df)) vec <- c(df[[colname]], vec)
   new_row <- max(nrow(df), length(vec))
   new_df <- df[1:new_row, ,drop = FALSE]
   new_df[colname] <- c(vec, rep(NA, new_row - length(vec)))
   new_df[is.na(new_df)] <- 0
   rownames(new_df) <- NULL
   new_df
}
Dpeaks = c(4, 1, 0, 9, 20, 4, 18, 11, 9, 6)
df <- add_peaks(df, Dpeaks, "Dpeaks")
df
#   Apeaks Dpeaks
#1       1      4
#2       6      1
#3       5      0
#4       0      9
#5       0     20
#6       0      4
#7       0     18
#8       0     11
#9       0      9
#10      0      6

Dpeaks = c(2, 1, 4)
df <- add_peaks(df, Dpeaks, "Dpeaks")
df

#  Apeaks Dpeaks
#1       1      4
#2       6      1
#3       5      0
#4       0      9
#5       0     20
#6       0      4
#7       0     18
#8       0     11
#9       0      9
#10      0      6
#11      0      2
#12      0      1
#13      0      4