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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:38:54+00:00 2026-06-04T13:38:54+00:00

As per Row wise matrix operations in R I would like to apply a

  • 0

As per Row wise matrix operations in R I would like to apply a row-wise function on a data.table I have. I wish to calculate, per row, the mean of a number of columns in that row. My current attempt is:

columns <- c(1,5,10,15,20) # Actually obtained via grep
my.data.table[,"average" := mean(columns),with=FALSE] # Or...
my.data.table[,average := mean(columns)]

This, unfortunately, simply returns the mean of the ‘columns’ vector rather than mean of the columns to which they refer. Is there a way to refer to these columns by number?

Here’s the average I’m trying to achieve:

key  a b c average
A    5 5 5 5
B    1 2 3 2
C    2 4 9 5
  • 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-04T13:38:55+00:00Added an answer on June 4, 2026 at 1:38 pm

    Here are two possible solutions. They are basically both from the link that you’ve already provided, so maybe I missed something with this question. Here we go:

    Solution 1 (using rowMeans):

    library(data.table)
    N <- 1000000
    my.data.table <- data.table(ID = 1:N,
                                Year1 = rnorm(N),
                                Year2 = rnorm(N),
                                Year3 = rnorm(N),
                                Year4 = rnorm(N))
    
    x <- c(2, 3, 4, 5)
    system.time(x1 <- rowMeans(my.data.table[, ..x]))
       user  system elapsed 
       0.08    0.00    0.08
    

    Solution 2: Get it into long format first. I thought this was faster, mainly because of Matthew’s comment in the other question that says that data.table is meant for the DT[,mad(variable),by=group] syntax. I think I’m missing something, but don’t see what:

    library(reshape2)
    DT <- as.data.table(melt(as.data.frame(my.data.table), id.var="ID"))
    setkey(DT, ID)
    system.time(x2 <- DT[, mean(value), by="ID"][[2]])
       user  system elapsed 
      11.28    0.00   11.33 
    all.equal(x1, x2)
    [1] TRUE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table and I'd like to pull one row per id with
I have a table of data and have one checkbox per row for the
I would like to display data, three columns per row during my for. I
I have a table of data in rows with a check box per row,
I have a table that currently has two columns per row and I need
I have a HTML table with a column per row with this html code
I would like to be able to display two lines of text per row
I have a Table containing a number of images per row. The number of
I have a table that per row has some inputs and one edit button.
I would like to print out a html table- it should divide data into

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.