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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:55:33+00:00 2026-05-31T07:55:33+00:00

I have some python and numpy experience but have never used R before. I’m

  • 0

I have some python and numpy experience but have never used R before. I’m trying to help my wife with her R project since although she has a much better grasp on statistics, she has little programming experience. I’m finding the syntax and documentation of R very confusing.

The original thing We wanted to do was loop through a large data.frame, do a bunch of spacial calculations involving prior and subsequent records, a little trig and some quality checks on the data and generate a new object with the data. We then will get this new data into GIS


EDIT:
Just to be clear, the calculations in this example are just a placeholder, and are nothing like the actual calculations I needed to do.


Initially I tried something like this:

> result = list()
> for (i in 1:5) {
+   #Calculate some dummy data. The actual calculations are much more involved
+   param1 = i * 1.1
+   param2 = i * 5.3
+   param3 = i + a_value
+   # Now append these calculated values to some sort of object
+   sample = list(param1=param1,param2=param2,param3=param3)
+   result <- rbind(result,sample)
+ }
> print(result)
       param1 param2 param3
sample 1.1    5.3    12    
sample 2.2    10.6   13    
sample 3.3    15.9   14    
sample 4.4    21.2   15    
sample 5.5    26.5   16

The “sample” column seems un-necessary, but oh well, it looks good. Now to reference a single column…

> result$param2
NULL

???I tried getting rid of ‘sample’ by:

+   result <- rbind(result,list(param1=param1,param2=param2,param3=param3))
>
     param1 param2 param3
[1,] 1.1    5.3    12    
[2,] 2.2    10.6   13    
[3,] 3.3    15.9   14    
[4,] 4.4    21.2   15    
[5,] 5.5    26.5   16 
> result$param2
NULL

Perhaps this data frame thing will work. I changed the first line to:

result = data.frame()
>
   param1 param2 param3
2     1.1    5.3     12
21    2.2   10.6     13
3     3.3   15.9     14
4     4.4   21.2     15
5     5.5   26.5     16
> result$param2 # One column
[1]  5.3 10.6 15.9 21.2 26.5
> result[2,] #One row
   param1 param2 param3
21    2.2   10.6     13
> result[3,]$param3 # Single value
[1] 14

So it’s working, but I’m not sure what the 21 (row number?) is all about. If I have more rows, the 21st row is ‘211’.

Could someone tell me why the first case didn’t work, what the ’21’ is all about, and if there is a better way to do this. Much of what I’ve read indicates that loops in R are a sign you don’t know what you are doing, but the learning curve on the alternatives seems steep. This is also why the script takes an amazingly long time to run, even on a fast machine.

  • 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-31T07:55:35+00:00Added an answer on May 31, 2026 at 7:55 am

    The problem is that R works very differently than other programming languages. It generally is not very fast to use a loop. Instead use the vectorization that makes R easy to work with (but different than other languages). So for your problem I’d probably do:

    i=1:5
    data.frame(param1 = i * 1.1, param2 = i * 5.3, param3 = i*2+9)
    

    Also check out apply, lapply, sapply, ifelse, etc. Also note that many functions are vectorized and work readily on vectors.

    If you really wanted to fix up what you have you could use the following:

     result = list()
     for (i in 1:5) {
       #Calculate some dummy data. The actual calculations are much more involved
       param1 = i * 1.1
       param2 = i * 5.3
       param3 = 2*i+9
       # Now append these calculated values to some sort of object
       sample = list(param1=param1,param2=param2,param3=param3)
       result <- data.frame(rbind(result,sample))
       rownames(result) <- 1:nrow(result)
     }
     print(result)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've done some Python but have just now starting to use Ruby I could
For the last few days, I have been trying to use Python for some
I am using Python and Numpy to do some data analysis. I have a
I have been trying to get a project of mine to run but I
I have some Python code that works correctly when I use python.exe to run
I have some old python code that uses the pywin32 extensions. Starting out with
I have created some python code which creates an object in a loop, and
I have some really nice Python code to do what I need to do.
I have some code in a python string that contains extraneous empty lines. I
I have some global variables in a Python script. Some functions in that script

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.