R - Count numbers of certain values in each column

R - Count numbers of certain values in each column



I have found similar questions to mine, but none of them explains how to do that for each column of a dataframe.



I have a dataframe like this:


x1 = seq(12, 200, length=20)
x2 = seq(50, 120, length=20)
x3 = seq(40, 250, length=20)
x4 = seq(100,130, length=20)
x5 = seq(10, 300, length=20)

df = data.frame(V1=x1, V2=x2, V3=x3, V4=x4, V5=x5)



Now I want to get the number of values that are greater than 120 for each column.



I have tried:


nrow(df[,1] >120)



That didnt work, it says 0, but its not true, and also I want to do all columns automatically.




2 Answers
2



You can use tidyverse to solve this.


tidyverse


library(tidyverse)

df%>%
gather(x, value, V1:V5)%>%
group_by(x)%>%
tally(value > 120)

# A tibble: 5 x 2
x n
<chr> <int>
1 V1 9
2 V2 0
3 V3 12
4 V4 7
5 V5 12



Hope this helps.





Thanks! Thats a short and easy solution!
– Essi
Feb 5 at 18:15



returning the amount of elements greater than 120 only for the first column


df[df[,1] >120 ,1]

[1] 120.8421 130.7368 140.6316
[4] 150.5263 160.4211 170.3158
[7] 180.2105 190.1053 200.0000

length(df[df[,1] >120 ,1])

[1] 9



returning the amount of elements greater than 120 for all columns


cols <- vector()

for(i in 1:ncol(df))
cols[i] <- length(df[df[,i] >120 ,i])


cols

[1] 9 0 12 7 12





Thank you! That works for my example, but on my real dataframe, it somehow also counts exmpty rows. I have no clue why.
– Essi
Feb 5 at 18:14






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)