greenfields
greenfields

Reputation: 5

R find values in a dataframe which are smaller than values in another vector

Here is my data:

myvec <- c(9,12,18)
mytez1 <- c(5,10,15,0,3,13)
mytez2 <- c(7,14,12,8,2,17)
mytez3 <- c(5,9,11,16,19,20)
mytez <- data.frame(mytez1, mytez2, mytez3)

Therefore in the end i am using one vector (myvec) and one dataframe (mytez). My goal is to count all values in the dataframe (mytez) which are smaller than the values of the vector (myvec). Thereby, i want to find the counts for each column seperately. Accordingly, the first column of the dataframe (mytez1) should only be compared to the first value of myvec (which is 9 here), to find the amount of values which are smaller than 9 (which would be 3 values in mytez1). I am aware of how it works with sum:

sum(mytez[,1] < myvec[1], na.rm="TRUE")

but as i have a very large dataset, i need to find a loop to do it. when using sapply:

counts <- sapply(myvec, function(x)  sum(mytez<x))

it doesn't do it seperately as i would like to. Does somebody have a good idea on how to do it? Thanks a lot in advance!

Upvotes: 0

Views: 150

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389175

Here are few ways to do this in base R :

#1. Using sweep
colSums(sweep(mytez, 2, myvec, `<`))

#2. Using mapply
colSums(mapply(`<`, mytez, myvec))

#3. Transposing the dataframe
rowSums(t(mytez) < myvec)

#Output
#mytez1 mytez2 mytez3 
#     3      3      4 

Upvotes: 0

Related Questions