当前位置:网站首页>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
边栏推荐
- Ubuntu 20.04 installing mysql8.0 and modifying the MySQL password
- QT excel table read / write library - qtxlsx
- Review of arrays and pointers triggered by a topic
- 开餐馆
- Generation method and usage of coredump
- Using Visual Studio
- Common dynamic memory errors
- Shared memory synchronous encapsulation
- New title of PTA
- 90 后眼中的理想 L9:最简单的产品哲学,造最猛的爆款 | 指南斟
猜你喜欢
定位position(5种方式)
What moment makes you think there is a bug in the world?
semget No space left on device
Yolov4 coco pre train Darknet weight file
User defined data type - structure
Master XSS completely from 0 to 1
QT excel table read / write library - qtxlsx
Gif动画怎么在线制作?快试试这款gif在线制作工具
Source code analysis of zeromq lockless queue
Source code analysis of synergetics and ntyco
随机推荐
How to combine multiple motion graphs into a GIF? Generate GIF animation pictures in three steps
Installing QT plug-in in Visual Studio
弹性布局(display:flex;)属性详解
How to cut the size of a moving picture? Try this online photo cropping tool
Qlogsystem log system configuration use
Paddlepaddle paper reproduction course biggan learning experience
What moment makes you think there is a bug in the world?
Brain tree (I)
Qt: Pro project file
Using Visual Studio
Basic knowledge of pointer
Stack and queue
HMS core machine learning service realizes simultaneous interpretation, supports Chinese-English translation and multiple voice broadcast
Source code analysis of synergetics and ntyco
Is it safe to open an online stock account? Who knows
Usage of qlist
dmsetup命令
How to download and install Weka package
How to package rpm
Yolov4 coco pre train Darknet weight file