Home > Enterprise >  How to run if statements on a dataframe in a for loop in R
How to run if statements on a dataframe in a for loop in R

Time:09-05

I have a problem which i haven't been able to find a solution for on here. I have a dataframe which, very simplified, would look something like this (Is actually 50k x 12):

tag:   1273 1273 1273  1275  1275 1278  1278

dist:
   1002
   3212
   1252
  2152
    232
    582
    752

I need a for loop that basically prints out the number of times a number is bigger than e.g 1000. So that i preferably end up with a vector that says

tag:  1273 1275 1278

amount:   3  1  0

I haven't worked with loops in R before, only python, so any help is appreciated.

CodePudding user response:

I think you can try tapply with function sum

> tapply(dist > 1000, tag, sum)
1273 1275 1278
   3    1    0

or table

> table(dist > 1000, tag)[2, ]
1273 1275 1278
   3    1    0

Data

tag <- c(1273, 1273, 1273, 1275, 1275, 1278, 1278)
dist <- c(1002, 3212, 1252, 2152, 232, 582, 752)

CodePudding user response:

It's difficult to guess what your intended outcome looks like; is this what you're trying to do?

library(dplyr)

df <- data.frame(tags = c(1273,1273,1273,1275,1275,1278,1278),
                 dist = c(1002,3212,1252,2152,232,582,752))

df %>%
  group_by(tags) %>%
  tally(dist > 1000, name = "amount") 
#> # A tibble: 3 × 2
#>    tags amount
#>   <dbl>  <int>
#> 1  1273      3
#> 2  1275      1
#> 3  1278      0

# output as a vector
df %>%
  group_by(tags) %>%
  tally(dist > 1000, name = "amount") %>%
  pull(amount, tags)
#> 1273 1275 1278 
#>    3    1    0

Created on 2022-09-05 by the reprex package (v2.0.1)

If this isn't correct, what changes need to be made?

  • Related