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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:16:54+00:00 2026-05-19T01:16:54+00:00

I have mostly programmed in Python, but I am now learning the statistical programming

  • 0

I have mostly programmed in Python, but I am now learning the statistical programming language R. I have noticed some difference between the languages that tend to trip me.

Suppose v is a vector/array with the integers from 1 to 5 inclusive.

v[3]  # in R: gives me the 3rd element of the vector: 3
      # in Python: is zero-based, gives me the integer 4
v[-1] # in R: removes the element with that index
      # in Python: gives me the last element in the array

Are there any other pitfalls I have to watch out for?

  • 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-19T01:16:55+00:00Added an answer on May 19, 2026 at 1:16 am

    Having written tens of thousands of lines of code in both languages, R is just a lot more idiosyncratic and less consistent than Python. It’s really nice for doing quick plots and investigation on a small to medium size dataset, mainly because its built-in dataframe object is nicer than the numpy/scipy equivalent, but you’ll find all kinds of weirdness as you do things more complicated than one liners. My advice is to use rpy2 (which unfortunately has a much worse UI than its predecessor, rpy) and just do as little as possible in R with the rest in Python.

    For example, consider the following matrix code:

    > u = matrix(1:9,nrow=3,ncol=3)
    > v = u[,1:2]
    > v[1,1]
    [2] 1
    > w = u[,1]
    > w[1,1]
    Error in w[1, 1] : incorrect number of dimensions
    

    How did that fail? The reason is that if you select a submatrix from a matrix which has only one column along any given axis, R “helpfully” drops that column and changes the type of the variable. So w is a vector of integers rather than a matrix:

    > class(v)
    [1] "matrix"
    > class(u)
    [1] "matrix"
    > class(w)
    [1] "integer"
    

    To avoid this, you need to actually pass an obscure keyword parameter:

    > w2 = u[,1,drop=FALSE]
    > w2[1,1]
    [3] 1
    > class(w2)
    [1] "matrix"
    

    There’s a lot of nooks and crannies like that. Your best friend at the beginning will be introspection and online help tools like str,class,example, and of course help. Also, make sure to look at the example code on the R Graph Gallery and in Ripley’s Modern Applied Statistics with S-Plus book.


    EDIT: Here’s another great example with factors.

    > xx = factor(c(3,2,3,4))
    > xx
    [1] 3 2 3 4
    Levels: 2 3 4
    > yy = as.numeric(xx)
    > yy
    [1] 2 1 2 3
    

    Holy cow! Converting something from a factor back to a numeric didn’t actually do the conversion you thought it would. Instead it’s doing it on the internal enumerated type of the factor. This is a source of hard-to-find bugs for people who aren’t aware of this, because it’s still returning integers and will in fact actually work some of the time (when the input is already numerically ordered).

    This is what you actually need to do

    > as.numeric(levels(xx))[xx]
    [1] 3 2 3 4
    

    Yeah, sure, that fact is on the factor help page, but you only land up there when you’ve lost a few hours to this bug. This is another example of how R does not do what you intend. Be very, very careful with anything involving type conversions or accessing elements of arrays and lists.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have developed application for drawing some shapes (lines mostly) , now i need
I have been programming in Java since 2004, mostly enterprise and web applications. But
i have a unix c programming assignment in which i do some advanced C
I am an electronics student and I have programmed mostly in Assembly. Last night
I'm starting Calculus this semester. I've used programming (or scripting) languages before, mostly PHP
As a mostly self-taught programmer, I have never really had anyone explain why certain
In the past my development team we have mostly done waterfall development against an
I found the following rather strange. Then again, I have mostly used closures in
How would I go about this? I have a mostly static site, which is
I have a JFace editor that is mostly made up of a TreeViewer .

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.