Main features of R
Note
Before starting to work with R, it is advised to create a new dedicated directory where all the work should be included. In fact, if several projects are to be developed at the same time, every project should have its own directory.
For Linux, MacOS:
Warning
R is case-sensitive, thus in this example:
> a = 1
> A = 2
variables a and A are different variables:
Starting R
For linux:
Report bugs at <https://bugs.R-project.org>.
Quitting R
You can use
quit()orq().
Using parenthesis in quit() informs R that the command refers to a function and not to a variable.
Help in R
Tab key can be used to complete the commands:
Other useful commands
R for research in Astronomy
R is a powerful programming language and environment for statistical
computing and data analysis that has become increasingly important in modern
astrophysics. However, when you first install R, you gain access to only
its base functionality, which includes fundamental statistical and
graphical capabilities. The true strength of R lies in its extensive
ecosystem of additional packages—specialized collections of functions, data,
and documentation created by the R community. These packages extend R’s
capabilities far beyond its default installation, covering virtually every
area of astronomical data analysis and computational astrophysics. From
handling FITS files and astronomical catalogs with FITSio and
astrodatR, to performing sophisticated time series analysis of variable
stars and exoplanet transits with astroTS, to creating
publication-quality plots and sky maps with ggplot2 and mapproj, the
availability of thousands of well-documented packages on repositories like
CRAN (Comprehensive R Archive Network)
makes R an incredibly versatile tool for astrophysical research. Learning
how to discover, install, and use these packages effectively is therefore a
fundamental skill that will enable you to tackle everything from photometric
data reduction and spectral analysis to cosmological simulations and survey
data mining, transforming R from a basic statistical calculator into a
comprehensive platform for astronomical research.
A first glipmse of R in action
Try the following code:
# load a specific data set
> data(cars)
# get info about the data set
> ?cars
# show data
> cars
# display the structure of the data object
> str(cars)
# plot data
> plot(cars)
# fit a linear model
> linear_model <- lm( dist ~ speed , data=cars )
# display summary of the fitting result
> summary(linear_model)
# display model coefficients
> coef(linear_model)
# plot residuals against speed
> plot( resid(linear_model) ~ speed , data=cars )
# Note: plot() is a generic function: it acts as a dispatcher that
# examines the class of the object you pass to it. R looks for a
# specific method plot.classname().
# In this case: graphics:::plot.data.frame(cars)