创建一个应用于多个不同数据框每一行的函数在R中。

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

Creating a function to apply to every row of several different DFs in r

问题

我有一堆看起来像这样的巨大数据框:

   a b c d e f g
  -------------- 
1| 4 3 6 3 7 3 7
2| 4 6 2 5 6 2 1
3| 3 6 2 6 8 1 5

我需要一个可以应用于每个数据框的函数,但创建一个行和函数需要包括数据框的名称:

apf <- function(rowSums(**df**[, c(1, 3, 4, 7)]))

如何使数据框在函数中成为一个变量,以便我可以为每个数据框运行以下操作:

apply(dfn, 1, apf(dfn))

然后将输出放入新列中:

dfn$new_c <- apply(dfn, 1, apf(dfn))
英文:

I have a bunch of gigantic data frames that look something like:

   a b c d e f g
  -------------- 
1| 4 3 6 3 7 3 7
2| 4 6 2 5 6 2 1
3| 3 6 2 6 8 1 5

I need a function that I can apply to every df but making a rowsum function requires you to include the df name:

apf &lt;- function(rowSums(**df**[, c(1, 3, 4, 7)]))

How can I make df a variable in the function such that I can run the following for each df:

apply(dfn, 1, apf(dfn))

and then put the output in a new colun

dfn$new_c &lt;- apply(dfn, 1, apf(dfn))

答案1

得分: 1

创建好你的函数之后(参见下面的我的示例玩具),你可以将其应用于你的帧列表:

apf <- function(d) {
d$sums = rowSums(d[, c(1,3,4,7)])
return(d)
}

dfs = lapply(list(a=a, b=b, c=c), apf)

英文:

Once you create your function (See my toy example below), you can apply it to a list of your frames:

apf &lt;- function(d) {
  d$sums = rowSums(d[, c(1,3,4,7)])
  return(d)
}

dfs = lapply(list(a=a, b=b, c=c),apf)

答案2

得分: -1

在将数据框放入列表后:

df_list &lt;- list(A = df, B = df, C = df)

使用任何代替 rowSums 的函数,该函数接受一个参数。

数据

df_list &lt;- list(A = structure(list(a = c(4L, 4L, 3L), b = c(3L, 6L, 6L), 
    c = c(6L, 2L, 2L), d = c(3L, 5L, 6L), e = c(7L, 6L, 8L), 
    f = 3:1, g = c(7L, 1L, 5L)), class = &quot;data.frame&quot;, row.names = c(NA, 
-3L)), B = structure(list(a = c(4L, 4L, 3L), b = c(3L, 6L, 6L
), c = c(6L, 2L, 2L), d = c(3L, 5L, 6L), e = c(7L, 6L, 8L), f = 3:1, 
    g = c(7L, 1L, 5L)), class = &quot;data.frame&quot;, row.names = c(NA, 
-3L)), C = structure(list(a = c(4L, 4L, 3L), b = c(3L, 6L, 6L
), c = c(6L, 2L, 2L), d = c(3L, 5L, 6L), e = c(7L, 6L, 8L), f = 3:1, 
    g = c(7L, 1L, 5L)), class = &quot;data.frame&quot;, row.names = c(NA, 
-3L)))
英文:

After putting the data frames in a list

df_list &lt;- list(A = df, B = df, C = df)
sapply(df_list, rowSums)
      A  B  C
[1,] 33 33 33
[2,] 26 26 26
[3,] 31 31 31

Place any function instead rowSums that takes one argument.

Data

df_list &lt;- list(A = structure(list(a = c(4L, 4L, 3L), b = c(3L, 6L, 6L), 
    c = c(6L, 2L, 2L), d = c(3L, 5L, 6L), e = c(7L, 6L, 8L), 
    f = 3:1, g = c(7L, 1L, 5L)), class = &quot;data.frame&quot;, row.names = c(NA, 
-3L)), B = structure(list(a = c(4L, 4L, 3L), b = c(3L, 6L, 6L
), c = c(6L, 2L, 2L), d = c(3L, 5L, 6L), e = c(7L, 6L, 8L), f = 3:1, 
    g = c(7L, 1L, 5L)), class = &quot;data.frame&quot;, row.names = c(NA, 
-3L)), C = structure(list(a = c(4L, 4L, 3L), b = c(3L, 6L, 6L
), c = c(6L, 2L, 2L), d = c(3L, 5L, 6L), e = c(7L, 6L, 8L), f = 3:1, 
    g = c(7L, 1L, 5L)), class = &quot;data.frame&quot;, row.names = c(NA, 
-3L)))

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

发表评论

匿名网友

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

确定