Good morning, I am new to R and I was trying to do a function that allows me to compare all the data of a row with a certain value that is at the end of the table and that varies for each row.
I have to check and count if each value of each row is greater than that number. The code I wrote is the following but I have no feedback on R
counter=0
for (i in 1:500){
for(j in 1:750){
if (db_class[j,i]>db_class[j,751]==TRUE) {
counter=counter+1}
i=i+1
}
j=j+1
}
Hi!
To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:
In the spirit of the above guide, why not begin with imagining you have a simpler problem, like data that has smaller dimensions, something tangible like 3 cols by 3 rows. (as opposed to 500 x 751)
A B C D MEAN
4 3 6 7 5
2 4 5 8 4,75
4 6 7 9 6,5
5 7 6 1 4,75
If (A1 > Mean1) countrow1 = count1 +1...
if (B1>Mean1) countrow1= count1+1..
If (A2 > Mean1) countrow2= count2 +1...
if (B2>Mean1) countrow2= count2+1..
I need to create a new col that contain this count for every row
library(tidyverse)
(example_data <- tribble(
~A, ~B, ~C, ~D,
4, 3, 6, 7,
2, 4, 5, 8,
4, 6, 7, 9,
5, 7, 6, 1
) %>% mutate(MEAN = rowMeans(.)))
(example_data2 <- example_data %>% mutate(
rcount = rowSums(. > .[["MEAN"]])
))
# A tibble: 4 x 6
A B C D MEAN rcount
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 4 3 6 7 5 2
2 2 4 5 8 4.75 2
3 4 6 7 9 6.5 2
4 5 7 6 1 4.75 3
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.