Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8571789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:51:33+00:00 2026-06-11T18:51:33+00:00

I was trying to learn R using a book. I was trying to do

  • 0

I was trying to learn R using a book. I was trying to do an example where for each row of the matrix, the corresponding element of the vector will be either 1 or 0, depending on whether the majority of the
first d elements in that row is 1 or 0. The code used was:-

copymaj <- function(rw,d) {
   maj <- sum(rw[1:d]) / d
   return(if(maj > 0.5) 1 else 0)
}

 x
    [,1] [,2] [,3] [,4] [,5]
[1,]  1    0    1    1    0
[2,]  1    1    1    1    0
[3,]  1    0    0    1    1
[4,]  0    1    1    1    0

apply(x,1,copymaj,3)

It is showing the above error. Also if I do apply(x,1,copymaj(3)), still error is shown.

R 2.13 is installed
Please help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:51:34+00:00Added an answer on June 11, 2026 at 6:51 pm

    As @BenBarnes pointed out, you probably misspelled sum, I think you wrote sim instead of sum.

    I was able to reproduce your error by doing:

    copymaj0 <- function(rw,d) {
      maj <- sim(rw[1:d]) / d    # here you have sim, this causes the error 
      return(if(maj > 0.5) 1 else 0)
    }
    
    copymaj1 <- function(rw,d) {
      maj <- sum(rw[1:d]) / d    # here you have sum which works well for me 
      return(if(maj > 0.5) 1 else 0)
    }
    
    x <- matrix(c(1,0,1,1,0,
             1,1,1,1,0,
             1,0,0,1,1,
             0,1,1,1,0), ncol=5, byrow=TRUE)
    
    apply(x,1,copymaj0,3) # prints error
    Error in FUN(newX[, i], ...) : could not find function "sim"
    
    apply(x,1,copymaj1,3) # works well
    [1] 1 1 0 1
    

    I really think you misspelled sum.

    apply(x,1,copymaj1(3)) won’t work becase if you read ?apply you’ll see
    apply(X, MARGIN, FUN, ...), then apply(x,1,copymaj1(3)) wil produce an error because
    ... replaces the arguments to FUN (d=3 in your case) is missed. In order to pass optional arguments to your fun you have to use ... as in apply(x,1,copymaj1,3) but not using apply(x,1,copymaj1(3)).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn python. I am using 3.1.2 and the o'reilly book is
I was trying learn lex and yacc using the oreilly book. I tried following
I'm trying to learn assembly using Dr Paul Carter's pcasm book: http://www.drpaulcarter.com/pcasm/ The author
I'm trying to learn assembly using NASM, the pcasm-book.pdf from Dr Paul Carter -
I am trying to learn some PHP using the book titled PHP for Absolute
I'm trying to learn project automation and I'm using the book Pragmatic Project Automation
I am trying to learn Python by myself using Zed A.Shaw's book Learn Python
I'm trying to learn Scheme from the book The Little Schemer using DrScheme on
I'm following through the Learn Rails by Example book, and I'm trying to run
I am currently trying to learn a bit of Cocoa (using the book Cocoa

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.