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

  • SEARCH
  • Home
  • 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 8938089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:32:44+00:00 2026-06-15T10:32:44+00:00

I wrote the following function: x <- 1:4 y <- c(1,2,7,4) mydf <- data.frame(a=x,b=x,c=x,d=x,y=y)

  • 0

I wrote the following function:

x    <- 1:4
y    <- c(1,2,7,4)
mydf <- data.frame(a=x,b=x,c=x,d=x,y=y)

practice  <-  function(x) if( x[1] == x[5]) {

             foo <- (x[2])
             return(foo)

              } else {

              bar     <- x[3] + x[4]                
              foobar <- x[2] - bar

               return(foobar)
              }       

newmydf <- apply( mydf, 1, practice)

this works fine on the data.frame provided. However I have another data.frame and I keep getting

Error in x[3] + x[4] : non-numeric argument to binary operator

despite the following str()

'data.frame':   133 obs. of  19 variables:
 $  : chr   ...
 $ : int   ...
 $ : int  ...
 $ : int  ...
 $ : int   ...
 $ : int   ...
 $ : int   ...
 $ : int   ...
 $ : int  ...
 $ : int  ...
 $ : int   ...
 $ : int   ...
 $ : int  ...
 $ : int  ...
 $ : int   ...
 $ : int  ...
 $ : num  ...
 $ : int  
 $ : int  

What possible little slip ups can I be making? I have started with a new session and I still get the same issue.

  • 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-15T10:32:45+00:00Added an answer on June 15, 2026 at 10:32 am

    The values in the first column of second data frame are of type “character”. As a result, the command apply turns the whole data frame into a character matrix. The operator + is not defined for character values.

    There are two possible solutions:

    1. Transform the first column to type numeric (if possible):

      mydf[ , 1] <- as.numeric(mydf[ , 1])
      
    2. Do not use the first column of the data frame:

      apply(mydf[ , -1], 1, practice)
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created a class A and wrote the following function foo() class A {
I wrote the following function to view data in a grid from F# interactive:
I'm learning sml, and wrote the following simple function: (* Return a list with
I wrote the following function to verify log in data for user, but so
I wrote the following function. This function reads *.las point format and create a
I wrote following function void validateUser(void) { string uName; string uPassword; char c; map
I wrote the following function in haskell, as it will enumerate every integer: integers
I wrote the following: Object.prototype.length = function(){ var count = -1; for(var i in
I want to call the Sleep function on ASM. So I wrote the following:
I wrote the following function to check if my HTML5 openDatabase table is filled

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.