当前位置:网站首页>Basic syntax and common commands of R language
Basic syntax and common commands of R language
2022-06-25 15:10:00 【A window full of stars and milky way】
R In fact, it is just a tool for data analysis , So at the beginning, you don't need to learn how deep and how detailed , You just need to be able to meet your current needs , Later, learn slowly in practice .
After all, I want to R It is not easy to learn . The correct way is to learn while doing , It won't be google Turn over the document .
This film is mainly about R Basic syntax and common command operations
assignment
R Assignment adoption <- perhaps -> perhaps =, The first of the standards is recommended .
because R A function with the same name is built in c(), It is best not to use... In coding c As object name , Otherwise, some imperceptible problems may arise
a <- 133
"hello" -> b # Pay attention to either way , The greater than or less than sign points to the variable name
d = 'This' # This is not recommended , It may cause problems
a
b
d
133
'hello'
'This'
view help
help(mean)
# perhaps
?mean
Package installation and loading
# Get contains R Package library location
.libPaths()
# View the packages that have been installed
library()
# Installation package
install.packages("packagename")
# Load package
library(packagesname)
# View the loaded packages
(.packages())
# Unload the loaded package ( Note that it is not to delete the package )
detach("package:packagename")
# Delete package
remove.packages("packagename")
Data reading and saving
Read
# Read csv
data <- read.csv('.\\ statistical \\example\\ch1\\table1_1.csv')
head(data,6) # Before reading 6 Row data
# Read Excel data
library(xlsx) # Need to install xlsx package
data <- read.xlsx("file",n) # n Is the sequence number of the worksheet to import
# Read spss data
library(foregin) # Installed by default
data <- read.spss("file",use.value.labels=TRUE,as.data.frame=TRUE)
# Read R Format data
data <- load('.\\ statistical \\example\\ch1\\example1_1.RData')
| The student's name | statistical | mathematics | Marketing | Management | Accounting |
|---|---|---|---|---|---|
| Zhangqingsong | 68 | 85 | 84 | 89 | 86 |
| Wang Yuxiang | 85 | 91 | 63 | 76 | 66 |
| Tian Siyu | 74 | 74 | 61 | 80 | 69 |
| Xulina | 88 | 100 | 49 | 71 | 66 |
| Zhang Zhijie | 63 | 82 | 89 | 78 | 80 |
| Zhaoyingying | 78 | 84 | 51 | 60 | 60 |
preservation
# preservation R Format data
save(data,file = '.\\...\\name.Rdata')
# preservation csv Format data
write.csv(data,file = '.\\...\\name.csv')
# preservation xlsx Format
library(xlsx)
write.xlsx(data, "data.xlsx",sheet.name="sheet1")
if Conditional statements
if sentence
x <- 30L # R In language , Add... To a positive integer L To represent integer data ( Positive integer )
if(is.integer(x)) {
print("X is an Integer")
}
[1] "X is an Integer"
if…else sentence
y <- list('a', 'v', 'd')
if('a' %in% y){ # %in% Operator Check whether the element is in the vector
print('a is in list')
}else{ # Notice the else The statement is not in if In the curly brackets of
print('a is not in list')
}
[1] "a is in list"
x <- c("what","is","truth")
if("Truth" %in% x) {
print("Truth is found the first time")
} else if ("truth" %in% x) {
print("truth is found the second time")
} else {
print("No truth found")
}
[1] "truth is found the second time"
switch sentence
# Create a function , The input value and the selected function type to output the result .
centre <- function(x, type) {
switch(type,
mean = mean(x),
median = median(x),
trimmed = mean(x, trim = .1))
}
centre(c(1,2,4,5),'mean')
3
Loop statement
while loop
ant <- 2
while(ant<5){
print('hello')
ant = ant + 1
}
[1] "hello"
[1] "hello"
[1] "hello"
for loop
v <- LETTERS[1:4] # LETTERS by 26 A vector of capital letters .
for(i in v){
print(i)
}
[1] "A"
[1] "B"
[1] "C"
[1] "D"
repeat loop
i <- 1
sum <- 0
repeat
{
sum = sum + i
if( i >= 100) # If it has been added circularly to 100, Then use break Jump out of repeat loop
break
i <- i + 1
}
print(sum)
[1] 5050
next sentence
R Language exists next sentence , We can use when we want to skip the current iteration of the loop without terminating it next. encounter next when ,R The parser skips this iteration , And start the next iteration of the loop .
k <- LETTERS[1:6]
for ( i in k) {
if (i == "D") {
next
}
print(i)
}
[1] "A"
[1] "B"
[1] "C"
[1] "E"
[1] "F"
R Common constants
# 26 Capital letters
LETTERS
# 26 Lowercase letters
letters
# Abbreviate the month
month.abb
# The name of the month
month.name
# π value
pi
'A' 'B' 'C' 'D' 'E' 'F' 'G' 'H' 'I' 'J' 'K' 'L' 'M' 'N' 'O' 'P' 'Q' 'R' 'S' 'T' 'U' 'V' 'W' 'X' 'Y' 'Z'
'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' 'p' 'q' 'r' 's' 't' 'u' 'v' 'w' 'x' 'y' 'z'
'Jan' 'Feb' 'Mar' 'Apr' 'May' 'Jun' 'Jul' 'Aug' 'Sep' 'Oct' 'Nov' 'Dec'
'January' 'February' 'March' 'April' 'May' 'June' 'July' 'August' 'September' 'October' 'November' 'December'
3.14159265358979
边栏推荐
猜你喜欢

google_ Breakpad crash detection

Learning notes on February 5, 2022 (C language)

How to download and install Weka package

90 后眼中的理想 L9:最简单的产品哲学,造最猛的爆款 | 指南斟

Source code analysis of zeromq lockless queue

2022年广东高考分数线出炉,一个几家欢喜几家愁

How to cut the size of a moving picture? Try this online photo cropping tool

15 -- k points closest to the origin

QT loading third-party library basic operation

Common dynamic memory errors
随机推荐
Customization and encapsulation of go language zap library logger
If multiple signals point to the same slot function, you want to know which signal is triggered.
Design and implementation of thread pool
Source code analysis of zeromq lockless queue
HMS Core机器学习服务实现同声传译,支持中英文互译和多种音色语音播报
How to download and install Weka package
iconv_ Open returns error code 22
In 2022, the score line of Guangdong college entrance examination was released, and several families were happy and several worried
Ubuntu 20.04 installing mysql8.0 and modifying the MySQL password
QT opens the print dialog box in a text editor
Ideal L9 in the eyes of the post-90s: the simplest product philosophy, creating the most popular products
多张动图怎样合成一张gif?仅需三步快速生成gif动画图片
2022年广东高考分数线出炉,一个几家欢喜几家愁
QT excel table read / write library - qtxlsx
One question per day, punch in
Single user mode
google_ Breakpad crash detection
System Verilog — interface
14 -- validate palindrome string II
QT set process startup and self startup