在R中用于apply()函数的函数

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

A function for using in apply() in R

问题

有人知道如何定义一个函数,可以在apply()中使用,返回如下数值吗?data[i, data[i, 300]]。

data[i, 300] 返回我的数据中的列号,我想知道每一行这个列号的内容。

可以使用for循环来解决这个问题,如下所示:
for (i in 1:nrow(data)) {
value <- data[i, data[i, 300]]
}

但是对于1000万个观测来说,这需要太长时间。我正在寻找一种使用apply()的快速方法。

感谢您可以提供的任何帮助。

英文:

Is there anyone know how can I define a function to be used in apply() that return the following value? data[i,data[i,300]].

data[i,300] returns a column number in my data, and I am interested to know the content of this column number for each row.

It's easy to use for loop for addressing this issue as follows:
for (i in 1: nrow(data)) {
value<-data[i,data[i,300]] }

But it takes too much time for 10M observations. I am looking for a fast approach using apply().
I appreciate any help you can provide me.

答案1

得分: 1

对于上述示例数据,假设第300列的值包含了每一行中要检查的不同列号,你可以使用下面的代码来获得期望的输出:

df[cbind(1:nrow(df), df[[300]])]

这段代码会根据每一行的第300列的值,选择相应的列来提取数据,就像你描述的那样。

英文:

I am going to assume that the 300th column has different column numbers for each row and for each row you want to check the value in the column specified by this 300th column.

sample data:

set.seed(123)
df &lt;- data.frame(
    c1 = sample(letters, 100, replace = TRUE), 
    c2 = sample(letters, 100, replace = TRUE), 
    c3 = sample(letters, 100, replace = TRUE), 
    c4 = sample(letters, 100, replace = TRUE), 
    c5 = sample(letters, 100, replace = TRUE), 
    d = sample(1:5, size = 100, replace = TRUE)
)
&gt; head(df)
  c1 c2 c3 c4 c5 d
1  o  o  p  b  r 2
2  s  u  w  m  l 2
3  n  e  a  x  o 3
4  c  h  h  q  s 5
5  j  s  h  c  u 1
6  r  j  j  r  f 3

Thus for above sample, let's assume the 6th column, column d is the one that gives the column numbers, so the expected output should be df[1, 2], df[2, 2], df[3, 3], df[4, 5], df[5, 1], df[6, 3], which evaluates to c(&#39;o&#39;, &#39;u&#39;,&#39;a&#39;, &#39;s&#39;, &#39;j&#39;, &#39;j&#39;)

To get this we can use:

df[cbind(1:nrow(df), df[[6]])] # change 6 with 300 for your case

huangapple
  • 本文由 发表于 2023年2月18日 02:53:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75488270.html
匿名

发表评论

匿名网友

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

确定