Some special skills in R language
1 Modify the default prompt language
The languages that R default prompts are some people in English and some people in Chinese. This is because each person's system default language is different, and it can be modified through the following methods.
() #Show system language(LANG="en") # Replace the default language to English
2 Check the memory size consumed by R
You can use() # to get the memory size, but the value of this function is only with the Windows system.
() #get memory size[1] 341
3 Check the memory size of a dataset
> (mtcars) 7208 bytes > (mtcars)/1024 #Show in kb7 bytes
4. Line breaks in the code
The default carriage return is to run the code, which is automatically filled in Rstudio. For example, if you define a function, it will be automatically filled in {}, and then it will be run. You can use shift+enter #Bobo
function(x,y) { }
5 Display while assigning values
After the default assignment is completed, you can print the data by simply typing the variable name. You can use the following method to complete two operations in one step.
(x <- runif(10)) [1] 0.5795985 0.4661326 0.9730974 0.6697417 0.2431985 0.3988545 0.4064351 0.8403910 [9] 0.3136191 0.9979925
7 View source code
If you want to view the source code of the R function, just enter the function name without brackets.
> mean function (x, ...) UseMethod("mean") <bytecode: 0x0000023e3b8db998> <environment: namespace:base>
8 Use functions to modify the image
If you do not set the R mirror, a selection box will pop up every time you install the R package. You can set it through a function before installation. Use the chooseCRANmirror() function, and give the ind option a value. The value represents the mirror number.
> chooseCRANmirror() Secure CRAN mirrors 1: 0-Cloud [https] 2: Australia (Canberra) [https] 3: Australia (Melbourne 1) [https] 4: Australia (Melbourne 2) [https] 5: Australia (Perth) [https] 6: Austria [https] 7: Brazil (BA) [https] 8: Brazil (PR) [https] 9: Brazil (RJ) [https] 10: Brazil (SP 1) [https] 11: Brazil (SP 2) [https] 12: Bulgaria [https] 13: Canada (MB) [https] 14: Chile (Santiago) [https] 15: China (Beijing 2) [https] 16: China (Hefei) [https] 17: China (Hong Kong) [https] 18: China (Guangzhou) [https] > chooseCRANmirror(ind = 18)
9 Show more data
By default, R displays 1000 rows of data. If you want to display more, you can use the settings option.
> options('') $ [1] 1000 > options(''=2000) > options('') $ [1] 2000
10 Decimal points are retained by default
The default R displays 7 as a decimal. If you want to retain two decimal places by default, you can set the digits option.
> options('digits') $digits [1] 7 > options('digits'=2) > options('digits') $digits [1] 2
11 Pipeline
Use pipelines to make the code more concise without defining too many intermediate variables. The pipe symbol in R is "%>%". If you want to use a pipe, you need to load the magrittr package. In fact, the various packages produced by Rstudio support pipelines by default, and it is also OK if the tidyverse package is loaded.
> library(magrittr) > library(ggplot2) > mtcars %>% ggplot(aes(x=cyl,y=mpg,group=cyl))+geom_boxplot()
12 Split column data
Sometimes you can't remember the column names, or you are prone to misspelling. If you want the column names to be automatically filled, you can use the attach function, so that each column becomes an independent variable.
attach(mtcars) > cyl [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4 > mpg [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 10.4 [17] 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7 15.0 21.4
13 Default loading package
If some packages are used frequently and want to automatically load after starting R, you can set it in the configuration file, such as starting R every issue and automatically loading the ggplot2 package.
("~/.Rprofile") .First <- function() { library(ggplot2) }
14 Add additional extension package loading path for R
The loading directory of the default R package is in the .libPaths() directory, and of course you can add more paths to it.
> .libPaths() [1] "C:/Users/genom/Documents/R/win-library/4.0" [2] "C:/Program Files/R/R-4.0.3/library" > .libPaths(new = "C:/Users/genom/Desktop/nparFiles/") > .libPaths() [1] "C:/Users/genom/Desktop/nparFiles" "C:/Program Files/R/R-4.0.3/library"
15 Migrate R packages
If you need to install the R package on one device and install it on another device, first save the list of R package names on the A device and write a loop on the other device to install it.
#Save the name list on device Aoldip <- ()[,1] save(oldip,file = "") #Installation on device B;load("") newip <- ()[,1] for (i in setdiff(oldip,newip)) { (i) }
16 List functions in R package
If you want to view all functions in an R package, you can use the following statement.
ls(package:base)
17 Use the functions in the package without loading
If you load the R package and want to use the functions in it, you need to use the "package name::function name".
dplyr::filter()
18 Quickly get color
When drawing, if you want to quickly set a few different colors, it is difficult to generate colors. You can use the rainbow() function to quickly generate colors given a data.
> rainbow(6) [1] "#FF0000" "#FFFF00" "#00FF00" "#00FFFF" "#0000FF" "#FF00FF"
19 Blast data
As mentioned earlier, the attach function can turn each column into a separate variable, but this method is not recommended because it will make the variable environment very confusing. You can use a special pipe character "%$%" to achieve the same effect, which I call "blasting" the data
> library(magrittr) Warning message: Cheng Jibao‘magrittr'It's forRVersion3.6.3 Come to build > women %$% plot(weight,height)
20 Cleverly use example functions to learn drawing
The example function will help run the code in the R help document. Sometimes if you want to see how a function is used, you can run this example function directly.
> library(pheatmap) > example("pheatmap") phetmp> # Create test matrix phetmp> test = matrix(rnorm(200), 20, 10) phetmp> test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3 phetmp> test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2 phetmp> test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4 phetmp> colnames(test) = paste("Test", 1:10, sep = "") phetmp> rownames(test) = paste("Gene", 1:20, sep = "") phetmp> # Draw heatmaps phetmp> pheatmap(test)
21 Statistical calculation time
If you want to count the running time of a code, you can use the() function
> (runif(100000000)) user system Passing 2.75 0.08 2.83
21 Free the memory
R has its own memory recycling mechanism, so even if variables are deleted, the memory will not change immediately, so you can use the gc() function to free up memory.
> () [1] 297.56 > rm(list = ls()) > () [1] 298.54 > gc() used (Mb) gc trigger (Mb) max used (Mb) Ncells 1384255 74.0 4046672 216.2 4046672 216.2 Vcells 4288164 32.8 27057220 206.5 33821525 258.1 > () [1] 255.5
22 Delete all variables
#Show all variable contents> ls() #delete> rm(list=ls()) #Release memory> gc()
23 Restore the default dataset
If you accidentally delete the built-in dataset, or if you define a variable with the same name, the original dataset will be replaced.
head(mtcars) mpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 > mtcars=1:10 > mtcars [1] 1 2 3 4 5 6 7 8 9 10 > data("mtcars") > head(mtcars) mpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 >
24 Quickly get function option parameters
There are many option parameters for R function. If it is troublesome to call out the help document every time, you can use the args() function to quickly print out the option parameters of the function.
> args(heatmap) function (x, Rowv = NULL, Colv = if (symm) "Rowv" else NULL, distfun = dist, hclustfun = hclust, reorderfun = function(d, w) reorder(d, w), , symm = FALSE, revC = identical(Colv, "Rowv"), scale = c("row", "column", "none"), = TRUE, margins = c(5, 5), ColSideColors, RowSideColors, cexRow = 0.2 + 1/log10(nr), cexCol = 0.2 + 1/log10(nc), labRow = NULL, labCol = NULL, main = NULL, xlab = NULL, ylab = NULL, = FALSE, verbose = getOption("verbose"), ...) NULL
The above is a detailed explanation of the 24 efficient operation techniques of R language. For more information about the 24 efficient operation of R language, please pay attention to my other related articles!