簡體   English   中英

在多列上應用 R 函數並生成輸出

[英]Applying R function over multiple columns and generating output

我正在嘗試生成一個新的數據框,它基於另一個數據框的值。

我的舊數據框具有每個變量和觀察值的平均值,如下所示:

    var1   var2   var3
#1: 2.1    3.4    2.7
#2  1.1    3.6    2.2
#3  2.9    1.7    2.7
data <- structure(list(var1 = c(2.1, 1.1, 2.9), var2 = c(3.4, 3.6, 1.7
), var3 = c(2.7, 2.2, 2.7)), class = "data.frame", row.names = c(NA, -3L))

我的目標是創建一個數據框,該數據框應包含原始數據框中每個觀測值的 10 個觀測值。 這些觀察結果應該復制平均值。 它應該是這樣的

   var 1 var 2  var 3
#1  2     3      2
#1  2     3      2
#1  2     3      2
#1  2     3      3
#1  2     3      3
#1  2     3      3 
#1  2     4      3
#1  2     4      3
#1  2     4      3
#1  3     4      3

現在為了創建這些觀察,我正在使用這個函數:

my_func <- function(y){
  wert <- y
  werte <- wert
  werte2 <- floor(werte)
  werte3 <- floor(werte)+1
  werte4 <- round((werte-werte2)*10)
    werte5 <- round(10-(werte-floor(werte))*10)
    y <- as.vector(rep(werte2,werte5))
  z <- as.vector(rep(werte3,werte4))
  b <- c(y,z)
  b
  }

之后,我將此函數應用於數據並將其存儲到列表中:

myList<- list()
for (i in 1:ncol){
pp <- lapply(data[,i],my_func)
myList[[i]] <- pp
}

不幸的是,我在執行此操作時遇到錯誤:

rep(werte2,werte5) 中的錯誤:'times' 參數無效
調用自:as.vector(rep(werte2,werte5))

有沒有辦法解決這個問題或更好的方法?

嘗試這個:

my_func <- function(x) {
  int_x <- as.integer(floor(x))
  dec_x <- as.integer(x * 10 - int_x * 10)
  out <- vapply(
    seq_along(x), 
    function(i, a, b) rep(a[[i]], 10L) + c(rep(0L, 10L - b[[i]]), rep(1L, b[[i]])), 
    integer(10L), int_x, dec_x
  )
  `attributes<-`(out, NULL)
}
as.data.frame(lapply(df, my_func))

輸出

> as.data.frame(lapply(df, my_func))
   var1 var2 var3
1     2    3    2
2     2    3    2
3     2    3    2
4     2    3    3
5     2    3    3
6     2    3    3
7     2    4    3
8     2    4    3
9     2    4    3
10    3    4    3
11    1    3    2
12    1    3    2
13    1    3    2
14    1    3    2
15    1    4    2
16    1    4    2
17    1    4    2
18    1    4    2
19    1    4    3
20    2    4    3
21    2    1    2
22    3    1    2
23    3    1    2
24    3    2    3
25    3    2    3
26    3    2    3
27    3    2    3
28    3    2    3
29    3    2    3
30    3    2    3

我認為您需要這樣的功能:

unmean <- function(vec, n = 10) {
  as.numeric(sapply(vec,  function(x) {
    c(rep(floor(x), round(n * (1 - x %% 1))), 
      rep(ceiling(x), round(n * (x %% 1))))
  }))
}

這允許您執行以下操作,例如:

unmean(2.5, n = 2)
#> [1] 2 3

unmean(3.2, n = 5)
#> [1] 3 3 3 3 4

unmean(c(2.1, 6.7), 10)
#> [1] 2 2 2 2 2 2 2 2 2 3 6 6 6 7 7 7 7 7 7 7

因此,對於您的解決方案,您將執行以下操作:

as.data.frame(lapply(data, unmean))
#>    var1 var2 var3
#> 1     2    3    2
#> 2     2    3    2
#> 3     2    3    2
#> 4     2    3    3
#> 5     2    3    3
#> 6     2    3    3
#> 7     2    4    3
#> 8     2    4    3
#> 9     2    4    3
#> 10    3    4    3
#> 11    1    3    2
#> 12    1    3    2
#> 13    1    3    2
#> 14    1    3    2
#> 15    1    4    2
#> 16    1    4    2
#> 17    1    4    2
#> 18    1    4    2
#> 19    1    4    3
#> 20    2    4    3
#> 21    2    1    2
#> 22    3    1    2
#> 23    3    1    2
#> 24    3    2    3
#> 25    3    2    3
#> 26    3    2    3
#> 27    3    2    3
#> 28    3    2    3
#> 29    3    2    3
#> 30    3    2    3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM