x <- read.table(header=T, text=" factorLevel characteristicValue A1 8.44 A1 8.36 A1 8.28 A2 8.59 A2 8.91 A2 8.6 A3 9.34 A3 9.41 A3 9.69 A4 8.92 A4 8.92 A4 8.74") head(x)
tmp <- textConnection( "day A B 0 10.0 10.0 7 9.0 9.1 14 8.0 8.2 21 7.0 7.3 28 6.0 6.4 35 5.0 5.5 42 4.0 4.5 49 3.0 3.6 56 2.0 2.7") x <- read.table(tmp, header=TRUE) close.connection(tmp) head(x)
df1 = data.frame(CustomerId=c(1:6),Product=c(rep("Toaster",3),rep("Radio",3))) df2 = data.frame(CustomerId=c(2,4,6),State=c(rep("Alabama",2),rep("Ohio",1))) df1;df2 #outer join: merge(x = df1, y = df2, by = "CustomerId", all = TRUE) #left outer: merge(x = df1, y = df2, by = "CustomerId", all.x=TRUE) #right outer: merge(x = df1, y = df2, by = "CustomerId", all.y=TRUE) #cross join: merge(x = df1, y = df2, by = NULL)
> #outer join: > merge(x = df1, y = df2, by = "CustomerId", all = TRUE) CustomerId Product State 1 1 Toaster <NA> 2 2 Toaster Alabama 3 3 Toaster <NA> 4 4 Radio Alabama 5 5 Radio <NA> 6 6 Radio Ohio > > #left outer: > merge(x = df1, y = df2, by = "CustomerId", all.x=TRUE) CustomerId Product State 1 1 Toaster <NA> 2 2 Toaster Alabama 3 3 Toaster <NA> 4 4 Radio Alabama 5 5 Radio <NA> 6 6 Radio Ohio > > #right outer: > merge(x = df1, y = df2, by = "CustomerId", all.y=TRUE) CustomerId Product State 1 2 Toaster Alabama 2 4 Radio Alabama 3 6 Radio Ohio > > #cross join: > merge(x = df1, y = df2, by = NULL) CustomerId.x Product CustomerId.y State 1 1 Toaster 2 Alabama 2 2 Toaster 2 Alabama 3 3 Toaster 2 Alabama 4 4 Radio 2 Alabama 5 5 Radio 2 Alabama 6 6 Radio 2 Alabama 7 1 Toaster 4 Alabama 8 2 Toaster 4 Alabama 9 3 Toaster 4 Alabama 10 4 Radio 4 Alabama 11 5 Radio 4 Alabama 12 6 Radio 4 Alabama 13 1 Toaster 6 Ohio 14 2 Toaster 6 Ohio 15 3 Toaster 6 Ohio 16 4 Radio 6 Ohio 17 5 Radio 6 Ohio 18 6 Radio 6 Ohio >
tmp <- textConnection( "豌蟆一 蠍一螳 襷 豺 6 4 7 6 5 5 7 5 6 6 5 3 4 5 6 3 3 2 3 4 4 3 3 3 2") x <- read.table(tmp, header=TRUE) close.connection(tmp) stack(x)
> stack(x) values ind 1 6 豌蟆一 2 5 豌蟆一 3 5 豌蟆一 4 3 豌蟆一 5 4 豌蟆一 6 4 7 7 8 3 9 3 10 3 11 7 蠍一螳 12 5 蠍一螳 13 4 蠍一螳 14 2 蠍一螳 15 3 蠍一螳 16 6 襷 17 6 襷 18 5 襷 19 3 襷 20 3 襷 21 5 豺 22 6 豺 23 6 豺 24 4 豺 25 2 豺
tmp <- textConnection( "豌蟆一 蠍一螳 襷 豺 6 4 7 6 5 5 7 5 6 6 5 3 4 5 6 3 3 2 3 4 4 3 3 3 2") x <- read.table(tmp, header=TRUE) close.connection(tmp) head(x)
> x[order(x$豺), ] 豌蟆一 蠍一螳 襷 豺 5 4 3 3 3 2 4 3 3 2 3 4 1 6 4 7 6 5 2 5 7 5 6 6 3 5 3 4 5 6 > x[order(x$豺, x$襷), ] 豌蟆一 蠍一螳 襷 豺 5 4 3 3 3 2 4 3 3 2 3 4 1 6 4 7 6 5 3 5 3 4 5 6 2 5 7 5 6 6
subset(x, select=c(豺, 蠍一螳), subset= (豌蟆一 > 5)) subset(x, select=-c(豺, 蠍一螳)) # 襷企(-) 覿碁 企 貉殊 誤 襾語 貉殊 襷.
> colnames(x) <- c("豌蟆磯", "", "蠍一螳", "襷", "豺") > x 豌蟆磯 蠍一螳 襷 豺 1 6 4 7 6 5 2 5 7 5 6 6 3 5 3 4 5 6 4 3 3 2 3 4 5 4 3 3 3 2
all.cols <- rbind(x1, x2) all.rows <- cbind(x1, x2) inner.join <- merge(x1, x2, by="join_key")
iris[, !names(iris) %in% c ("Species" , "Petal.Width")]
`%notin%` <- Negate(`%in%`) iris[, names(iris) %notin% c ("Species" , "Petal.Width")]--https://www.r-bloggers.com/the-notin-operator/
> mat <- matrix(1:10, nrow=5) > mat [,1] [,2] [1,] 1 6 [2,] 2 7 [3,] 3 8 [4,] 4 9 [5,] 5 10 > dimnames(mat) <- list(c(1:5), c(paste("Var", 1:2, sep="."))) > mat Var.1 Var.2 1 1 6 2 2 7 3 3 8 4 4 9 5 5 10 > vec1 <- LETTERS[1:5] > vec2 <- letters[1:5] > vec1 [1] "A" "B" "C" "D" "E" > vec2 [1] "a" "b" "c" "d" "e" > df <- data.frame(mat, vec1, name=I(vec2)) > df Var.1 Var.2 vec1 name 1 1 6 A a 2 2 7 B b 3 3 8 C c 4 4 9 D d 5 5 10 E e > df <- data.frame(mat, vec1, 貉朱=I(vec2)) > df Var.1 Var.2 vec1 貉朱 1 1 6 A a 2 2 7 B b 3 3 8 C c 4 4 9 D d 5 5 10 E e > > df <- data.frame(mat, vec1, name=I(vec2), row.names=1) > df Var.2 vec1 name 1 6 A a 2 7 B b 3 8 C c 4 9 D d 5 10 E e > df <- data.frame(mat, vec1, name=I(vec2), row.names=vec2) > df Var.1 Var.2 vec1 name a 1 6 A a b 2 7 B b c 3 8 C c d 4 9 D d e 5 10 E e >
> colA <- c(1:5) > colB <- c(6:10) > data.frame(colA, colB) colA colB 1 1 6 2 2 7 3 3 8 4 4 9 5 5 10
id x y id1 1 2 id2 3 4 id3 5 6
> data.f <- read.table(file="c:\\example3.txt", header=TRUE) 危 read.table(file = "c:\\example3.txt", header = TRUE) : more columns than column names
> #flush=FALSE 蟆曙 > data.f <- read.table(file="c:\\example3.txt", skip=1, col.names=c("id", "x", "y"), flush=FALSE) > data.f id x y 1 id1 1 2 2 id2 3 4 3 id3 5 6 > #flush=TRUE 蟆曙 > data.f <- read.table(file="c:\\example3.txt", skip=1, col.names=c("id", "x", "y"), flush=TRUE) > data.f id x y 1 id1 1 2 2 id3 5 6
> data.f <- read.table(file="c:\\example2.txt", header=TRUE) > data.f x y id1 1 2 id2 3 4 id3 5 NA id4 miss 7 >
> data.f <- read.table(file="c:\\example2.txt", header=TRUE, na.strings=("miss")) > data.f x y id1 1 2 id2 3 4 id3 5 NA id4 NA 7 >
> data.f <- read.table(file="c:\\example2.txt", header=TRUE, na.strings=("miss"), row.names=1) > data.f x y id1 1 2 id2 3 4 id3 5 NA id4 NA 7 >
df <- rs[1:10,] tname <- "dbo.predict_result" library(RODBC) fast_dbinsert <- function(df, tname){ #企 query <- paste0( "if object_id('", tname, "') is not null drop table ", tname) sqlQuery(conn, query) sqlSave(conn, df[0:0,], tablename = tname, rownames=FALSE) #一危 tmp_filename <- tempfile() write.table(df, tmp_filename, na = "\\N", row.names = FALSE, col.names = FALSE, quote = FALSE, sep = "\t") sqlQuery(conn, query) unlink(tmp_filename) }