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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:15:22+00:00 2026-06-06T07:15:22+00:00

I would like to add a column to every data frame in my R

  • 0

I would like to add a column to every data frame in my R environment which all have the same format.
I can create the column I want with a simple assignment like this:

x[,8] <- x[,4]/(x[,4]+x[,5])

When I try to put this in a for loop that will iterate over every object in the environment, I get an error.

control_data <- ls()

for (i in control_data) {(i[,8] <- i[,4]/(i[,4]+i[,5]))}

Error: unexpected ‘[‘ in "for (i in control_data) {["

Here is what the input files look like:

ENSMUSG00000030088  Aldh1l1 chr6:90436420-90550197  1.5082200   3.130860    0.671814    0.0000000
ENSMUSG00000020932  Gfap    chr11:102748649-102762226   7.0861500   44.182700   20.901700   0.2320750
ENSMUSG00000024411  Aqp4    chr18:15547902-15562193 3.4920400   3.474880    2.463230    0.0331238
ENSMUSG00000023913  Pla2g7  chr17:43705046-43749150 1.5105400   24.275600   11.422400   1.5111100
ENSMUSG00000035805  Mlc1    chr15:88786313-88809437 1.9010200   7.147400    5.313190    0.6358940
ENSMUSG00000007682  Dio2    chr12:91962993-91976878 1.7322900   12.094200   6.738320    1.0736900
ENSMUSG00000017390  Aldoc   chr11:78136469-78141283 55.4562000  199.958000  91.328300   22.9541000
ENSMUSG00000005089  Slc1a2  chr2:102498815-102630941    63.7394000  130.729000  103.710000  10.0406000
ENSMUSG00000070880  Gad1    chr2:70391128-70440071  2.6501400   14.907500   13.730200   1.3992200
ENSMUSG00000026787  Gad2    chr2:22477724-22549394  3.9908200   11.308600   28.221500   1.4530500

Thank you for any help you could provide. Is there a better way to do this using an apply function?

  • 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-06T07:15:23+00:00Added an answer on June 6, 2026 at 7:15 am

    As mentioned in the comment, your error happens because the results of calling ls are not the objects themselves but rather their names as strings.

    To use the for-loop, you’ll be headed down the eval(parse(...)) path. You can also do this with apply and a function.

    myfun <- function(x) {
      df <- get(x)
      df[,8] <- df[,4] / (df[,4] + df[,5])
      return(df)
    }
    
    control_data <- ls()
    
    lapply(control_data, myfun)
    

    As per the comment:

    for(i in control_data) {
      df <- get(i)
      df[,8] <- df[,4] / (df[,4] + df[,5])
      assign(i, df)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to add a column from behind code to a data bound
I have a dataframe and would like to add a new column where the
I have own project and i would like add for this class same as
I have millions of lines generated from data updated every second which look like
I have several columns in a MySQL database that I would like to add
I have a ListView and each item have a TextView. I would like add
I would like to add a GestureDetector to all views (view groups) of an
I would like to add a custom calculation method to a managed object (which
I would like to add roots to a VirtualTreeView http://www.delphi-gems.com/index.php/controls/virtual-treeview with a thread like
I would like to add a hash into an array using Ruby version 1.8.7:

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.