day1_practice3
# " " – 공백 (default: sep=" ") myname <- "Jason Bourne" paste("My name is", myname, sep=" ") # file name file_id = 1533 paste("Dataset_",file_id, ".txt",sep="") # vector arguments paste(LETTERS[1:5],1:5) # vector 인수들을 원소별로 결합 후 collapse 정의 문자로 구분 # (default) collapse = NULL paste("A","B",collapse='/') paste(c("A","B"),collapse='/') paste(c("A","B"),1:2,collapse='/') paste(c("A","B"),1:2,sep='?', collapse='/') # paste(vector1, ..., sep=‘’,collapse= NULL) paste(c("A","B"),1:2,sep='',collapse='/') paste0(c("A","B"),1:2,collapse='/') paste0(LETTERS[1:5],letters[1:5]) test <- c("abcdefg", "AFFY1245820") nchar(test) f_name <- "AFFY1245820" substr(f_name,5,nchar(f_name)) strtrim("ABCDEF",3) strtrim(rep("abcdef", 3), c(1,4,10)) strsplit(c("a,b,c,d","cd,e"),split=',') strsplit(c("010-1234-1111","010-1222-3434"),split='-') x <- c("asfef", "yuiop[", "b", "stuff.blah.yech") strsplit(x, "e") strsplit(x,"[a-e]") strsplit(x,"[aleu]") unlist(strsplit("a.b.c", ".")) unlist(strsplit("a.b.c", "[.]")) unlist(strsplit("a.b.c", ".", fixed = TRUE)) test = dir("./") grep("^[A-Ca-c][a-z]",test,value=T) grep(".exe$",test,value=T) dir("./") getwd() setwd("../") dir() dir.create('./test'); dir() x <- c(1,3,2,4) xmat <- matrix(c(1,2,4,5,2,1),3,2) dat = data.frame() # 비어있는 데이터 프레임 dat = edit(dat) a = readline("Input any integer: ") a b = readline("Input two integers with comma :") # Input two integers with comma : 3,5 as.numeric(unlist(strsplit(b,"[,]"))) setwd('/BiO/example/dataset') x = scan(file="input_noh.txt",what=numeric()) head(x) x = scan(file="input_noh.txt",what=character()) head(x) x = scan(file="input_noh.txt") head(x) x = scan(file="input_h.txt") head(x) x = scan(file="input_h.txt",what=character()) head(x) dat = read.table(file="input_noh.txt") dat2 = read.table(file="input_noh.txt",header=T) dat3 = read.table(file="input_h.txt",header=F) dat4 = read.table(file="input_h.txt",header=T) x <- 1:10 cat(x,file="x.txt",sep="\n") # output to a file cat(x,sep="\t") # output to a screen cat("\n ",1,"st element of x = ",x[1]) # "\n" – 줄바꿈, "\t" – 탭 x1 <- 1:20 x2 <- rep(c("A","B","B","A"),5) x3 <- rep(c(T,F),each=10) dat <- cbind(x1,x2,x3) # 문자열로 모두 변환됨. dat <- data.frame(x1,x2,x3) write.table(dat,file="test1.txt",row.names=T, col.names=T,quote=T,sep="\t") write.table(dat,file="test2.txt",row.names=F, col.names=F,quote=F,sep="\n") write.table(dat,file="test3.txt",sep=",") library(xlsx) dat = read.xlsx(file='food.xlsx',sheetIndex=1, as.data.frame=T,header=T,startRow=1) head(dat) save(list=ls(),file='test.rdata') load(file = 'test.rdata') x <- matrix(c(NA, 1, 3, NA, NA, 2),3,2) is.na(x) sum(is.na(x)) # number of missing values which(is.na(x)) # 1차원 위치 which(is.na(x),arr.ind = T) # 2 차원 위치 which(is.na(x),T) # 2차원 위치 v = c("a,b,c,b,e","a,b","d,e,f") lapply(v,nchar) sp = strsplit(v,',') sp lapply(sp,length) lapply(sp,paste0,3) v = c("a,b,c,b,e","a,b","d,e,f") sapply(v,nchar) sp = strsplit(v,',') sp sapply(sp,length) ?mtcars head(mtcars) tapply(mtcars$mpg,mtcars$cyl,mean) tapply(mtcars$mpg,mtcars$am,mean) library(dplyr) filter(mtcars,cyl==4,am==1) filter(mtcars,wt>2,cyl==8) names(mtcars) select(mtcars,mpg,cyl,wt) select(mtcars,mpg,-hp,-gear) mtf = select(mtcars,mpg,cyl,hp,wt) mutate(mtf,hw_rat=hp/wt) mutate(mtf,hw_rat=hp/wt,cw_rat=cyl/wt) mtf = select(mtcars,mpg,am,cyl,wt) arrange(mtf,mpg,desc(cyl)) arrange(mtf,am,cyl,wt) summarize(mtcars,m_mpg=mean(mpg),v_mpg=var(mpg)) summarize(mtcars,md_mpg=median(mpg),md_wt=median(wt)) summarize(mtcars,trows=range(mpg)) mt_gr = group_by(mtcars,cyl) head(mt_gr) summarize(mtcars,m_mpg=mean(mpg),m_wt=mean(wt)) summarize(mt_gr,m_mpg=mean(mpg),m_wt=mean(wt)) dat <- group_by(mtcars, cyl) dat <- select(dat, mpg, wt, cyl) dat <- summarize(dat, m_mpg = mean(mpg)) dat <- filter(dat, m_mpg > 16) dat dat2 <- mtcars %>% group_by(cyl) %>% select(mpg, wt, cyl) %>% summarize(m_mpg = mean(mpg)) %>% filter(m_mpg > 16) dat2
day1_practice3.txt · Last modified: 2021/03/17 13:09 by 127.0.0.1