Background MaterialL4get the current working directory >getwd() #same in MAC read the csv (comma-seperated values) file >read.csv("xx.csv") list the file and directory under the current directory >dir() change the working directory >setwd("F:/workspace/") To list out all the functions in one script >ls()Week1L4assignment >x <- 5 comments # hi, this is a commentL55 basic "atomic" classes
L6c() function is to create a vector: x <- c(0.5,0.6) x <- c(True, Flase) vector() function acts alike, with initial value 0 for numeric. x <- vector("numeric", length = 10) mixed classes x<- c(True, 3) # will be numeric Explicit Coercion > x<-0:2 > class(x) [1] "integer" > as.logical(x) [1] FALSE TRUE TRUE we use list() to create list. >y<-list("a", 1, TRUE)L7create matrix >m<-matrix(nrows=2, ncols=3) >attributes(m) $dim [1] 2 3 matrix is column wise change vector to matrix >m<-1:10 >dim(m)<-c(2,5) or use cbind or rbind >x<-1:3 >y<-10:12 >cbind(x,y) 1 10 2 11 2 12 >rbind(x,y) similar but in rowL8factors are ordered or unordered, like key for the php array, factors could be treated as numeric vector with labels give an example >x<-factor(c("yes","yes","no")) >x [1] yes yes no Levels: no yes >table(x) yes no 2 1 >unclass(x) 221 that is how factor x expressed in R underneath! The first level is called the baseline level, it is determined by the alphabet rank, however, you could change the order. >x<-factor(c("yes","yes","no"),levels=c("yes","no")) and yes will be in the first place.L9-Missing Valuesis.na() is.nan() NaN value all be treated as NA, but the converse is not true.L10-Data Framestabular 表格的 data frames ~ matrices list ~ vector Yes, that the class doesn't matter in data frames. Special attributes: row.names created by read.table() or read.csv() convert to matrix data.matrix() an example: >x<- data.frame(foo=1:4, bar = c(T,T,F,F)) >x foo bar 1 1 TRUE 2 2 TRUE 3 3 FALSE 4 4 FALSEL10-Names Attribute>x >- 1:3 > names(x) <- c("foo","bar","norf") >x foo bar norf 1 2 3L12-Reading Tabular Data
read.table
L12-Reading Large Tablesset the arguments! all numeric, one is fine: colClasses = "numeric"L13-Textual Data FormatL17-Subsetting-Basics>x <- c("a","b","c") >x[1] [1] "a" > x[1:3] >x[x>"a"] >u <- x>"a" >u [1] FALSE TRUE TRUEL17-Subsetting-Lists>x <- list (foo =1:4, bar =0.6) >x[1] $foo [1] 1 2 3 4 we got a list! >x[[1]] [1] 1 2 3 4 we got a sequence! >x$bar [1] 0.6 >x[["bar"]] #this is equal >x["bar"] # we got a list >x[c(1, 3)] >name = "foo" >x[[name]] this is usefulL17-Subsetting-Matrices>x[1, ] # missing is fine No dropping forcing: >x[1, 2, drop =FALSE]L18-partial matching>x<-list(aardvark=1:5) >x$a [1] 1 2 3 4 5 >x[["a"]] NULL >x[["a", exact = FALSE]] [1] 1 2 3 4 5L19-Removing NA Values>x <- c(1, 2, NA, 4, NA, 5) >bad <- is.na(x) >x[!bad] >good<-complete.cases(x, y)L20-Vectorized Operationsmatrix x*y #by rank x%*%y # by true matrix multiplicationWeek1L2 if-elseif(x>3) { y<-10 }else{ y<-0 } also true: y<- if(x>3) { 10 }else{ 0 }L2 For loopsfor (i in 1:10){ } x <- c("a","b","c","d") for (i in seq_along(x)){ print(x[i]) } for (letter in x){ print(letter) }L4 Functionsset default value: abc = function(a = 10){ } columnmean <- function(y, removeNA = TRUE){ nc <- ncol(y) means <- numeric(nc) for(i in 1:nc) { mean[i] <- mean(y[,i], na.rm = removeNA) } }L6 Functions... argument indicate a variable number of arguments that are usually passed on to other funcitons. myplot <- function(x, y, type = "l", ...) { plot (x, y, type = type, ...) } explicityly matching after dot dot dotL7 Functions could be made dynamically!lexical vs. dynamical scoping make.power <- function(n) { pow <- function(x) { x^n } pow } >cube <- make.power(3) # note cube is a function > cube(3) [1] 27 ls(environment(cube))L8 code styleindenting 缩进L10 Date and timesx <- as.Date("1970-01-01") x <- Sys.time() |
Pointhere, we can see the sytle is very like windows/dos cute assignment sign same comment sign as bash complex! Like fortran interesting... Attributes! Like NCL! be careful with c(), not like other language in fact, I think this is really convenient merge vectors to matrix, this is really user-friendly! Impressive! like PHP array, but much easier to understand Like NCL or MATLAB See it? Plenty of data types, very user-friendly. You could imagine how simple when use data frames to process EXCEL type files. Briliant!!! Very like $$ in ncl Dim down or not, it is a problem |
Measure |
Try to push paperhub project on Lab server to github. The following are steps and barriers when I did this.
create a new repo on github, same name as “paperhub”
Try one file down_enw.php
$git init # to regard paperhub dir as a git repo $git add down_enw.php $git commit -m "first commit" $git push origin master
Get error:
$ git push origin master
error: The requested URL returned error: 403 Forbidden while accessing https://github.com/Novarizark/paperhub.git
fatal: HTTP request failed
Follow this:
http://jim-y.me/linux/2015/04/01/setting-up-git-for-error-403-forbidden/ http://stackoverflow.com/questions/7438313/pushing-to-git-returning-error-code-403-fatal-http-request-failed
Change:
$vi .git/config url = https://Novarizark@github.com/Novarizark/paperhub.git :wq
Another error:
(gnome-ssh-askpass:29241): Gtk-WARNING **: cannot open display:
Follow this: http://stackoverflow.com/questions/16077971/git-push-produces-gtk-warning#
unset SSH_ASKPASS
Done!
A few days ago I have found that the available disk space on Teamserver was shrunk to 60%. This is very weird because most datasets on the machine were uploaded by me and I know they only occupy about 30% in 22TB. Once I thought they might come from one of my teammate, uploading CMIP5 data or something. However, when I checked individual directory in home, I found nothing. Then I checked “ll -ah” in home. Holly shit! One file named “.xsession-error” takes up about 6TB, and still enlarging! So I google it and found it might be a bug of Linux, regardless of the distribution. Many people delete it or use file permission to restrict its expansion, without any source seeking. I followed them, while nothing happened to the available size from df command. I used DirStat and the original software to check this, nothing. What the fuck! I just let it alone and do my staff until I found there is only 2TB available today. Another similar file named as “.xsession-error.XXXXX” did the evil things. In fact this file was a text file, thus I “tail” it and found the flowing statement:
sslvpn_read_packet: SSL_read failed! Error while reading SSL packet keep alive recv failed! TCP_KEEP_ALIVE_SOCK_ENVENT sslvpn_read_packet: SSL_read failed! Error while reading SSL packet keep alive recv failed! TCP_KEEP_ALIVE_SOCK_ENVENT sslvpn_read_packet: SSL_read failed!
VPN! So it must be the vpn soft which aimed at connecting TH-2 that wrongly redirect the std out flow to this poor file and made the shit. Without any hesitate, I uninstall the VPN soft. Everything’s back to normal.