R:仅当(相同)行(不同)列中的值为true时,才在[row,column]中添加值

时间:2022-03-23 07:37:26

This is what I'm trying to code for in R...

这就是我想在R中编写的代码...

Let's say I have 50 rows and 4 columns. If the value in (row 1, column 2) was greater than 5, then count the value in (row 1, column 4).

假设我有50行和4列。如果(第1行,第2列)中的值大于5,则计算(第1行,第4列)中的值。

For an example: (row,column)

例如:(行,列)

If (1,2) = (6) then count the value in (1,4)

如果(1,2)=(6)则计算(1,4)中的值

If (2,2) = (8) then count the value in (2,4)

如果(2,2)=(8)则计算(2,4)中的值

If (3,2) = (4) then DO NOT count the value in (3,4)

如果(3,2)=(4)则不计算(3,4)中的值

And so on....Then add the all the values from column 4.

依此类推....然后添加第4列中的所有值。

How would I code this in R? I've tried creating a function, looping, if statements, etc.

我将如何在R中编码?我试过创建一个函数,循环,if语句等。

1 个解决方案

#1


1  

This shouldn't be too hard! Just subset your data.frame or matrix (however your data is stored) to include only the values of the fourth column, only taking the rows for which the second column is greater than five.

这不应该太难!只需将data.frame或matrix(无论您的数据存储在一起)的子集仅包含第四列的值,只获取第二列大于5的行。

sum(yourDataFrame[yourDataFrame[ ,2] > 5, 4])

Because many R functions are vectorized, it is often far easier (and faster!) to use vectorized functions like sum() than to loop. yourDataFrame[ ,2] > 5 will return a logical vector. Applying sum() to that vector will treat the TRUE entries as 1 and the FALSE entries as 0, thus providing a count of the values in yourDataFrame[ ,2] that are greater than 5.

因为许多R函数是矢量化的,所以使用像sum()这样的矢量化函数比循环更容易(也更快!)。 yourDataFrame [,2]> 5将返回逻辑向量。将sum()应用于该向量会将TRUE条目视为1,将FALSE条目视为0,从而提供yourDataFrame [,2]中大于5的值的计数。

#1


1  

This shouldn't be too hard! Just subset your data.frame or matrix (however your data is stored) to include only the values of the fourth column, only taking the rows for which the second column is greater than five.

这不应该太难!只需将data.frame或matrix(无论您的数据存储在一起)的子集仅包含第四列的值,只获取第二列大于5的行。

sum(yourDataFrame[yourDataFrame[ ,2] > 5, 4])

Because many R functions are vectorized, it is often far easier (and faster!) to use vectorized functions like sum() than to loop. yourDataFrame[ ,2] > 5 will return a logical vector. Applying sum() to that vector will treat the TRUE entries as 1 and the FALSE entries as 0, thus providing a count of the values in yourDataFrame[ ,2] that are greater than 5.

因为许多R函数是矢量化的,所以使用像sum()这样的矢量化函数比循环更容易(也更快!)。 yourDataFrame [,2]> 5将返回逻辑向量。将sum()应用于该向量会将TRUE条目视为1,将FALSE条目视为0,从而提供yourDataFrame [,2]中大于5的值的计数。