字符串鏈接函數pastesql
> paste("Hello","world") [1] "Hello world"
> paste("A", 1:6, sep = "") [1] "A1" "A2" "A3" "A4" "A5" "A6"
> paste(letters[1:6],collapse=",") [1] "a,b,c,d,e,f"
> paste("A", 1:6, sep = "", collapse=",") [1] "A1,A2,A3,A4,A5,A6"
> paste(letters[1:4], seq='_') [1] "a _" "b _" "c _" "d _"
> path <- 'E:\\mytestdata\\' > setwd(path) > dir.data <- dir(path) > dir.data [1] "a.csv" "b.csv" "c.csv" "d.csv" >for(i in 1:length(dir.data)){ >data <- read.csv(paste(path,dir.data[i],sep='')) >}
write.csv(a.csv,file=paste(path,'data.csv'))
paste('select * from student where date =','date',sep=" ")
> date1 <- 20150101 > date2 <- 20151231 > paste0("select * from sales where stat_date between ",date1," and ",date2) [1] "select * from sales where stat_date between 20150101 and 20151231"
dim(x)查看或設置數組的維度向量數組
> a <- matrix(1:20,nrow=5,ncol=4,byrow=T) > a [,1] [,2] [,3] [,4] [1,] 1 2 3 4 [2,] 5 6 7 8 [3,] 9 10 11 12 [4,] 13 14 15 16 [5,] 17 18 19 20 > is.matrix(a) [1] TRUE > dim(a) #查看或設置數組的維度向量 [1] 5 4 # 錯誤的用法 > dim(a) <- c(4,4) Error in dim(a) <- c(4, 4) : dims [product 16]與對象長度[20]不匹配 > # 正確的用法 > a <- 1:20 > dim(a) <- c(5,4) #轉換向量爲矩陣 > a [,1] [,2] [,3] [,4] [1,] 1 6 11 16 [2,] 2 7 12 17 [3,] 3 8 13 18 [4,] 4 9 14 19 [5,] 5 10 15 20
> is.character(a) [1] FALSE > is.numeric(a) [1] TRUE > is.matrix(a) [1] TRUE > is.data.frame(a) [1] FALSE # 矩陣轉換爲data.frame > is.data.frame(as.data.frame(a)) [1] TRUE