有没有一种方法可以编辑此功能以将数字添加到同一列?

时间:2020-07-06 19:28:40

标签: 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_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
}

现有数据框

Apeaks
1
6
5

整数

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

更新的数据框

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

谢谢您的帮助!

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

如果我们可以从cbind.fill访问rowr

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

或使用base R,将向量保持在list

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

数据

df1 <- structure(list(Apeaks = c(1L, 6L, 5L)), 
    class = "data.frame", row.names = c(NA, 
-3L))
相关问题