Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...