创建一个使用矢量化函数的新数据框。

huangapple go评论95阅读模式
英文:

building a new data frame with vectorized function

问题

我试图从现有的数据框中通过在几列上执行重复计算来构建一个新的数据框。目前我有一个类似这样的解决方案:

  1. library(tidyverse)
  2. iris_avg <- data.frame(SLength = colMeans(matrix(iris$Sepal.Length, nrow = 10)),
  3. SWidth = colMeans(matrix(iris$Sepal.Width, nrow = 10)),
  4. PLength = colMeans(matrix(iris$Petal.Length, nrow = 10)),
  5. PWidth = colMeans(matrix(iris$Petal.Width, nrow = 10)))
  6. > iris_avg
  7. SLength SWidth PLength PWidth
  8. 1 4.86 3.31 1.45 0.22
  9. 2 5.21 3.65 1.42 0.25
  10. 3 5.01 3.39 1.55 0.27
  11. 4 5.07 3.46 1.42 0.20
  12. 5 4.88 3.33 1.47 0.29
  13. 6 6.10 2.87 4.37 1.38
  14. 7 5.85 2.65 4.14 1.27
  15. 8 6.26 2.85 4.49 1.41
  16. 9 5.83 2.75 4.27 1.34
  17. 10 5.64 2.73 4.03 1.23
  18. 11 6.57 2.94 5.77 2.04
  19. 12 6.55 2.90 5.54 2.05
  20. 13 6.63 2.96 5.50 1.93
  21. 14 6.74 3.04 5.62 1.94
  22. 15 6.45 3.03 5.33 2.17

我觉得应该有一种简单的方法可以使用类似lapply或map的东西,但是我一直在努力让它工作,因为我是R的新手。非常感谢任何建议!

英文:

I am trying to build a new data frame from an existing dataframe by performing a repitive calculation across a few columns. Currently I have a solution that looks something like this:

  1. library(tidyverse)
  2. iris_avg &lt;- data.frame(SLength = colMeans(matrix(iris$Sepal.Length, nrow = 10)),
  3. SWidth = colMeans(matrix(iris$Sepal.Width, nrow = 10)),
  4. PLength = colMeans(matrix(iris$Petal.Length, nrow = 10)),
  5. PWidth = colMeans(matrix(iris$Petal.Width, nrow = 10)))
  6. &gt; iris_avg
  7. SLength SWidth PLength PWidth
  8. 1 4.86 3.31 1.45 0.22
  9. 2 5.21 3.65 1.42 0.25
  10. 3 5.01 3.39 1.55 0.27
  11. 4 5.07 3.46 1.42 0.20
  12. 5 4.88 3.33 1.47 0.29
  13. 6 6.10 2.87 4.37 1.38
  14. 7 5.85 2.65 4.14 1.27
  15. 8 6.26 2.85 4.49 1.41
  16. 9 5.83 2.75 4.27 1.34
  17. 10 5.64 2.73 4.03 1.23
  18. 11 6.57 2.94 5.77 2.04
  19. 12 6.55 2.90 5.54 2.05
  20. 13 6.63 2.96 5.50 1.93
  21. 14 6.74 3.04 5.62 1.94
  22. 15 6.45 3.03 5.33 2.17

I feel like there should be a simple way to use something like lapply or map, but I have struggled to get it to work, as I am new to R. Any advice would be greatly appreciated!

答案1

得分: 2

以下是代码的翻译部分:

  1. # 使用基本的R语言,可以将这个操作压缩成一行代码:
  2. as.data.frame(lapply(iris[1:4], \(x) sapply(split(x, 0:149 %/% 10), mean)))
  3. #&gt; Sepal.Length Sepal.Width Petal.Length Petal.Width
  4. #&gt; 0 4.86 3.31 1.45 0.22
  5. #&gt; 1 5.21 3.65 1.42 0.25
  6. #&gt; 2 5.01 3.39 1.55 0.27
  7. #&gt; 3 5.07 3.46 1.42 0.20
  8. #&gt; 4 4.88 3.33 1.47 0.29
  9. #&gt; 5 6.10 2.87 4.37 1.38
  10. #&gt; 6 5.85 2.65 4.14 1.27
  11. #&gt; 7 6.26 2.85 4.49 1.41
  12. #&gt; 8 5.83 2.75 4.27 1.34
  13. #&gt; 9 5.64 2.73 4.03 1.23
  14. #&gt; 10 6.57 2.94 5.77 2.04
  15. #&gt; 11 6.55 2.90 5.54 2.05
  16. #&gt; 12 6.63 2.96 5.50 1.93
  17. #&gt; 13 6.74 3.04 5.62 1.94
  18. #&gt; 14 6.45 3.03 5.33 2.17

希望这有所帮助。

英文:

You could squeeze this into a single line of code in base R:

  1. as.data.frame(lapply(iris[1:4], \(x) sapply(split(x, 0:149 %/% 10), mean)))
  2. #&gt; Sepal.Length Sepal.Width Petal.Length Petal.Width
  3. #&gt; 0 4.86 3.31 1.45 0.22
  4. #&gt; 1 5.21 3.65 1.42 0.25
  5. #&gt; 2 5.01 3.39 1.55 0.27
  6. #&gt; 3 5.07 3.46 1.42 0.20
  7. #&gt; 4 4.88 3.33 1.47 0.29
  8. #&gt; 5 6.10 2.87 4.37 1.38
  9. #&gt; 6 5.85 2.65 4.14 1.27
  10. #&gt; 7 6.26 2.85 4.49 1.41
  11. #&gt; 8 5.83 2.75 4.27 1.34
  12. #&gt; 9 5.64 2.73 4.03 1.23
  13. #&gt; 10 6.57 2.94 5.77 2.04
  14. #&gt; 11 6.55 2.90 5.54 2.05
  15. #&gt; 12 6.63 2.96 5.50 1.93
  16. #&gt; 13 6.74 3.04 5.62 1.94
  17. #&gt; 14 6.45 3.03 5.33 2.17

The following explanation should make this a bit clearer. Instead of using an anonymous function, we can define a function that takes a vector (or data frame column), splits it into chunks of length 10, and uses sapply to get the average of each chunk in a single vector:

  1. mean_of_every_10_items &lt;- function(x) {
  2. groups_numbers &lt;- (seq_along(x) - 1) %/% 10
  3. groups_of_10 &lt;- split(x, group_numbers)
  4. return(sapply(groups_of_10, mean))
  5. }

We can apply this function to each numeric column of iris using lapply, to get a list containing the result of our function on each column. As a last step, we turn this list back into a data frame:

  1. iris[1:4] |&gt;
  2. lapply(mean_of_every_10_items) |&gt;
  3. as.data.frame()
  4. #&gt; Sepal.Length Sepal.Width Petal.Length Petal.Width
  5. #&gt; 0 4.86 3.31 1.45 0.22
  6. #&gt; 1 5.21 3.65 1.42 0.25
  7. #&gt; 2 5.01 3.39 1.55 0.27
  8. #&gt; 3 5.07 3.46 1.42 0.20
  9. #&gt; 4 4.88 3.33 1.47 0.29
  10. #&gt; 5 6.10 2.87 4.37 1.38
  11. #&gt; 6 5.85 2.65 4.14 1.27
  12. #&gt; 7 6.26 2.85 4.49 1.41
  13. #&gt; 8 5.83 2.75 4.27 1.34
  14. #&gt; 9 5.64 2.73 4.03 1.23
  15. #&gt; 10 6.57 2.94 5.77 2.04
  16. #&gt; 11 6.55 2.90 5.54 2.05
  17. #&gt; 12 6.63 2.96 5.50 1.93
  18. #&gt; 13 6.74 3.04 5.62 1.94
  19. #&gt; 14 6.45 3.03 5.33 2.17

The "one-liner" version does the same thing, but it's a lot harder to see what it's doing.

<sup>Created on 2023-07-17 with reprex v2.0.2</sup>

答案2

得分: 2

在tidyverse中:

  1. library(tidyverse)
  2. iris %>%
  3. reframe(across(-Species, ~colMeans(matrix(.x, 10))))

另一种基本R的方法是找到正确的分组,并在tapply中使用它:

  1. x <- data.matrix(iris[-5])
  2. tapply(x, list((row(x) - 1) %/% 10, col(x)), mean)

另一种方法是使用sapply + split

  1. t(sapply(split(iris[-5], gl(nrow(iris)/10, 10)), colMeans))

希望这可以帮助你。

英文:

in tidyverse:

  1. library(tidyverse)
  2. iris %&gt;%
  3. reframe(across(-Species, ~colMeans(matrix(.x, 10))))
  4. Sepal.Length Sepal.Width Petal.Length Petal.Width
  5. 1 4.86 3.31 1.45 0.22
  6. 2 5.21 3.65 1.42 0.25
  7. 3 5.01 3.39 1.55 0.27
  8. 4 5.07 3.46 1.42 0.20
  9. 5 4.88 3.33 1.47 0.29
  10. 6 6.10 2.87 4.37 1.38
  11. 7 5.85 2.65 4.14 1.27
  12. 8 6.26 2.85 4.49 1.41
  13. 9 5.83 2.75 4.27 1.34
  14. 10 5.64 2.73 4.03 1.23
  15. 11 6.57 2.94 5.77 2.04
  16. 12 6.55 2.90 5.54 2.05
  17. 13 6.63 2.96 5.50 1.93
  18. 14 6.74 3.04 5.62 1.94
  19. 15 6.45 3.03 5.33 2.17

Another base R approach is to find correct groupings and use that within tapply:

  1. x &lt;- data.matrix(iris[-5])
  2. tapply(x, list((row(x) -1 ) %/% 10, col(x)), mean)
  3. 1 2 3 4
  4. 0 4.86 3.31 1.45 0.22
  5. 1 5.21 3.65 1.42 0.25
  6. 2 5.01 3.39 1.55 0.27
  7. 3 5.07 3.46 1.42 0.20
  8. 4 4.88 3.33 1.47 0.29
  9. 5 6.10 2.87 4.37 1.38
  10. 6 5.85 2.65 4.14 1.27
  11. 7 6.26 2.85 4.49 1.41
  12. 8 5.83 2.75 4.27 1.34
  13. 9 5.64 2.73 4.03 1.23
  14. 10 6.57 2.94 5.77 2.04
  15. 11 6.55 2.90 5.54 2.05
  16. 12 6.63 2.96 5.50 1.93
  17. 13 6.74 3.04 5.62 1.94
  18. 14 6.45 3.03 5.33 2.17

Another way using sapply+ split

  1. t(sapply(split(iris[-5], gl(nrow(iris)/10, 10)), colMeans))
  2. Sepal.Length Sepal.Width Petal.Length Petal.Width
  3. 1 4.86 3.31 1.45 0.22
  4. 2 5.21 3.65 1.42 0.25
  5. 3 5.01 3.39 1.55 0.27
  6. 4 5.07 3.46 1.42 0.20
  7. 5 4.88 3.33 1.47 0.29
  8. 6 6.10 2.87 4.37 1.38
  9. 7 5.85 2.65 4.14 1.27
  10. 8 6.26 2.85 4.49 1.41
  11. 9 5.83 2.75 4.27 1.34
  12. 10 5.64 2.73 4.03 1.23
  13. 11 6.57 2.94 5.77 2.04
  14. 12 6.55 2.90 5.54 2.05
  15. 13 6.63 2.96 5.50 1.93
  16. 14 6.74 3.04 5.62 1.94
  17. 15 6.45 3.03 5.33 2.17

huangapple
  • 本文由 发表于 2023年7月18日 01:50:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706963.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定