简体   繁体   中英

Applying R function over multiple columns and generating output

I'm trying to generate a new dataframe which is based on the values form another one.

My old dataframe has mean values for each variable and observation and looks like this:

    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))

My goal is to create a dataframe that should include 10 observations for each observation in the original dataframe. Those observations should replicate the mean values. It should look like this

   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

Now to create those observations, I'm using this function:

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
  }

Afterwards, I'm applying this function to the data and storing it into a list:

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

Unfortunately, I'm getting an error executing this:

Error in rep(werte2, werte5) : invalid 'times' argument
Called from: as.vector(rep(werte2, werte5))

Is there a way to fix this or a better approach?

Try this:

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))

Output

> 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

I think you need a function like this:

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))))
  }))
}

This allows you to do for example:

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

So for your solution you would do:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM