PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, November 1, 2022

[FIXED] How to compute cosine for loop with function from *apply family?

 November 01, 2022     apply, loops, performance, r     No comments   

Issue

I need convert this for loop into small because it's taking more time for 50k accounts and I need to improve performance by using a function from the *apply family.

a <- data.frame(a=c(1, 2, 2, 3), b=c(2, 3, 4, 5))
b <- data.frame(a=c(1), b=c(2))

##### **by using apply function I need convert this code**
library('lsa')
res <- c()
for (i in 1:nrow(a)) {
  cosinevalue <- cosine(as.numeric(a[i, ]), as.numeric(b))[1]
  res <- rbind(res, as.numeric(cosinevalue))
}

a$cosinevalue <- res

Solution

Using vapply.

vapply(seq_len(nrow(a)), \(i) lsa::cosine(unlist(a[i, ]), unlist(b)), numeric(1))
# [1] 1.0000000 0.9922779 1.0000000 0.9970545

Or Vectorizeing it, which is three times faster.

Vectorize(lsa::cosine, vectorize.args='x')(as.data.frame(t(a)), unlist(b)) |> unname()
# [1] 1.0000000 0.9922779 1.0000000 0.9970545


Answered By - jay.sf
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing