简体   繁体   中英

for statement: >= not working inside loop in R

I have the following code to count the number of values that are >= to 5.0 in one of the columns of my dataframe:

x <- c(0)
for (value in df6$X..Area) {
    print(value)
    if (value >= 5) {
      x  <- x + 1
      print(x)
    }
    else {
      x <- x + 0
      print(x)
    } 
  } 
print(x)  

And it returns x=10 when I know it should be 17:

[1] "11.73098473"
[1] 0
[1] "0"
[1] 0
[1] "9.849058487"
[1] 1
[1] "43.2290388"
[1] 1
[1] "12.8687744"
[1] 1
[1] "0"
[1] 1
[1] "16.30132657"
[1] 1
[1] "2.034024167"
[1] 1
[1] "9.728666256"
[1] 2
[1] "6.917304127"
[1] 3
[1] "23.29546703"
[1] 3
[1] "52.92463306"
[1] 4
[1] "25.28850095"
[1] 4
[1] "2.38663933"
[1] 4
[1] "76.27067973"
[1] 5
[1] "51.24831201"
[1] 6
[1] "53.32903266"
[1] 7
[1] "50.15874772"
[1] 8
[1] "2.532038435"
[1] 8
[1] "70.50085659"
[1] 9
[1] "32.78354366"
[1] 9
[1] "53.89978124"
[1] 10
[1] "1.18394304"
[1] 10
> print(x)  
[1] 10

However, when I take this out of the loop and test for values individually, it returns the right answer?

> z <- 11.73098473
> z >= 5
[1] TRUE

So then why is the >= not working inside the loop? I am very new to R, can anyone help me with this, please? Thanks

A version avoiding the for-loop:

df6 <- data.frame(`X..Area` = as.character(runif(100, min = 0, max = 10)))
df6$X..Area <- as.numeric(df6$X..Area)
# df6$cumsum <- cumsum(df6$X..Area >= 5)
# print(df6$cumsum)

df6$gt5 <- df6$X..Area >= 5
colSums(df6[, "gt5", drop = FALSE])

# alternative:
# sum(df6$X..Area >= 5)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM