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 3349230
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:38:20+00:00 2026-05-18T01:38:20+00:00

I would appreciate insight into why this happens and how I might do this

  • 0

I would appreciate insight into why this happens and how I might do this more eloquently.

When I use sapply, I would like it to return a 3×2 matrix, but it returns a 2×3 matrix. Why is this? And why is it difficult to attach this to another data frame?

a <- data.frame(id=c('a','b','c'), var1 = c(1,2,3), var2 = c(3,2,1))
out <- sapply(a$id, function(x) out = a[x, c('var1', 'var2')])
#out is 3x2, but I would like it to be 2x3
#I then want to append t(out) (out as a 2x3 matrix) to b, a 1x3 dataframe
b <- data.frame(var3=c(0,0,0))

when I try to attach these,

b[,c('col2','col3')] <- t(out)

The error that I get is:

Warning message:
In `[<-.data.frame`(`*tmp*`, , c("col2", "col3"), value = list(1,  :
  provided 6 variables to replace 2 variables

although the following appears to give the desired result:

rownames(out) <- c('col1', 'col2')
b <- cbind(b, t(out))

I can not operate on the variables:

b$var1/b$var2

returns

Error in b$var1/b$var2 : non-numeric argument to binary operator

Thanks!

  • 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-05-18T01:38:20+00:00Added an answer on May 18, 2026 at 1:38 am

    To expand on DWin’s answer: it would help to look at the structure of your out object. It explains why b$var1/b$var2 doesn’t do what you expect.

    > out <- sapply(a$id, function(x) out = a[x, c('var1', 'var2')])
    > str(out)  # this isn't a data.frame or a matrix...
    List of 6
     $ : num 1
     $ : num 3
     $ : num 2
     $ : num 2
     $ : num 3
     $ : num 1
     - attr(*, "dim")= int [1:2] 2 3
     - attr(*, "dimnames")=List of 2
      ..$ : chr [1:2] "var1" "var2"
      ..$ : NULL
    

    The apply family of functions are designed to work on vectors and arrays, so you need to take care when using them with data.frames (which are usually lists of vectors). You can use the fact that data.frames are lists to your advantage with lapply.

    > out <- lapply(a$id, function(x) a[x, c('var1', 'var2')])  # list of data.frames
    > out <- do.call(rbind, out) # data.frame
    > b <- cbind(b,out)
    > str(b)
    'data.frame':   3 obs. of  4 variables:
     $ var3: num  0 0 0
     $ var1: num  1 2 3
     $ var2: num  3 2 1
     $ var3: num  0 0 0
    > b$var1/b$var2
    [1] 0.3333333 1.0000000 3.0000000
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Would appreciate some insight isolating this, some semi-repeatable crashes in an iPhone app of
I would appreciate any help on this issue. Lets say I want to load
This is about my assignment so I would appreciate generic answers with explanation. I
I would appreciate some guidance on modelling services and operations in WCF. I have
I would appreciate some help with something I working on and have not done
I would appreciate some help with an UPDATE statement. I want to update tblOrderHead
I would appreciate some help on creating the proper SQL to retrieve only one
I would appreciate suggestions as to how to implement the following with Ninject: I
I have an interesting problem and would appreciate your thoughts for the best solution.
I'm new to regular expressions and would appreciate your help. I'm trying to put

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.