当前位置:网站首页>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
边栏推荐
- ‘make_ unique’ is not a member of ‘std’
- How to download and install Weka package
- AB string interchange
- Vs2019 scanf error
- Paddlepaddle paper reproduction course biggan learning experience
- Common dynamic memory errors
- (translation) json-rpc 2.0 specification (Chinese version)
- Qmake uses toplevel or topbuilddir
- Afterword of Parl intensive learning 7-day punch in camp
- Daily question, Caesar code,
猜你喜欢

QQ情话糖果情话内容获取并保存

Mining procedure processing

SPARQL learning notes of query, an rrdf query language

Arithmetic operations and expressions

How to download and install Weka package

Review of arrays and pointers triggered by a topic

15 -- k points closest to the origin

Dynamic memory allocation

Ubuntu 20.04 installing mysql8.0 and modifying the MySQL password

Learning notes on February 8, 2022 (C language)
随机推荐
‘make_ unique’ is not a member of ‘std’
Usage of qlist
QQ love talk candy love talk content acquisition and storage
Dmsetup command
Common dynamic memory errors
A deformation problem of Hanoi Tower
55 specific ways to improve program design (2)
What moment makes you think there is a bug in the world?
Biscuit distribution
QT database connection
Qcodeeditor - QT based code editor
One question per day,
QT excel table read / write library - qtxlsx
One question per day, a classic simulation question
Automatic correlation between QT signal and slot
High precision addition
How to download and install Weka package
Gif动画怎么在线制作?快试试这款gif在线制作工具
[C language] 32 keyword memory skills
QT inline dialog