---
# System prepended metadata

title: R and R-Studio
tags: [Statistical Analysis in Simple Steps Using R]

---

###### tags: `Statistical Analysis in Simple Steps Using R`

# R and R-Studio
## 1.4 Updating R

3 Solutions to update R on R-Studio

1. The first method is to download a new version of R from R website > CRAN. Then restart your RStudio. The new R version will be loaded automatically.

2. Windows only – use installr
~~~
install.packages("installr")
library(installr)
updateR()
~~~

3. Mac only – use updateR
~~~
install.packages('devtools') #assuming it is not already installed
library(devtools)
install_github('andreacirilloac/updateR')
library(updateR)
updateR(admin_password = 'Admin user password')
~~~

檢查 R 版本:
~~~
R.version

R.version.string
~~~

## 1.5 Installing, Loading, and Unloading R packages
~~~
install.packages("meta") ## Install package "meta"
library(meta) #load the package "meta"
help("meta-package")
detach("package:meta", unload=TRUE)
~~~
"#" is used to make a comment.

## 1.6 Getting Familiarity with R
**Object<-function** or
**Object=function**
~~~
x<-c(2,4,6,8)
x
y=seq(2,8,by=2)
y
~~~
where *c* is the **concatenate** function which groups or combines values into one object.

**Arithmetic Operation in R**
$+$ : addition
$-$ : subtraction
$*$ : multiplication
$/$ : division
$^$ or $**$ : exponentition

~~~
4+6
4-6
4*6
4/6
4^2
4^2-2*3
~~~

Generating 10 random numbers from a standard normal distribution
~~~
rnorm(10)
~~~

### Executing Functions
~~~
x=rnorm(10)
mean(x)
median(x)
sd(x)
~~~

## 1.7 Data Entry

### 1.7.1 Entering Data from keyboard
1. Creat an object with variable names along with their data type.
2. Invoke the text editor by typing "edit()", enter your data, and save the results back to the data object.

~~~
Wood=data.frame(name=character(0),height=numeric(0))
Wood2022=edit(Wood)
~~~

https://www1.nseindia.com/products/content/equities/indices/historical_index_data.htm

### 1.7.2 Importing data from a "TAB" delimited text file
~~~
nifty1=read.delim("C:/.../nifty1.txt")
View(nifty) ## View the dataframe-nifty
~~~

### 1.7.3 Importing data from a "csv" file 
~~~
nifty2=read.csv(":/.../nifty2.csv")
~~~

### 1.7.4 Importing data from a "excel" file
~~~
library(readxl)
nifty3=read_excel(":/.../nifty3.xlsx")
~~~
