_ | 覦覈襦 | 豕蠏手 | 殊螳 | 譯殊碁
FrontPage › DataFrame

Contents

1
2 覓語企 一危 襷り鍵
3 cube
4 join
5 na螳 貉
6 譟郁唄
7 alter colum
8 螳襦襯 碁襦
9
10 subset
11 (貉)企 覦蠑瑚鍵
12 na螳 蟇壱蠍
13 一危壱 豺蠍
14 轟 企 譟壱蠍
15 轟 企 觜手 譟壱蠍
16 轟 襷 觜手 譟壱蠍
17 襭 : data.frame()
18 襭 : read.table()
19 一危 sql server 觜襯願 ロ蠍


1 #

一危 覲() 蟯谿一(貊)襦 蟲焔 2谿 襭 螳豌伎企.

2 覓語企 一危 襷り鍵 #

覦覯1
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)

覦覯2
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)

3 cube #

as.data.frame(addmargins(xtabs(uptake ~ Plant + Type + Treatment, CO2)))

4 join #

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
> 

5 na螳 貉 #

tmp[, colSums(is.na(tmp)) == 0]

6 譟郁唄 #

ex1[ex1$hw==1,]

7 alter colum #

一危壱 覲蟆
x$殊 <- as.Date(x$殊)


x$殊 <- NULL

8 螳襦襯 碁襦 #

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     豺

9 #

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

10 subset #

subset(x, select=c(豺, 蠍一螳), subset= (豌蟆一 > 5))
subset(x, select=-c(豺, 蠍一螳)) # 襷企(-) 覿碁 企 貉殊 誤 襾語 貉殊 襷.

11 (貉)企 覦蠑瑚鍵 #

> 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

12 na螳 蟇壱蠍 #

clean < na.omit(x)

13 一危壱 豺蠍 #

all.cols <- rbind(x1, x2)
all.rows <- cbind(x1, x2)
inner.join <- merge(x1, x2, by="join_key")

14 轟 企 譟壱蠍 #

iris[, names(iris) %in% c ("Species" , "Petal.Width")]

15 轟 企 觜手 譟壱蠍 #

iris[, !names(iris) %in% c ("Species" , "Petal.Width")]

or

`%notin%` <- Negate(`%in%`)
iris[, names(iris) %notin% c ("Species" , "Petal.Width")]
--https://www.r-bloggers.com/the-notin-operator/

16 轟 襷 觜手 譟壱蠍 #

iris[!rownames(iris) %in% c(1, 2, 3),]


17 襭 : data.frame() #

data.frame()襯 伎 襭襯 ロ, 蟲譟 轟煙 蟆壱 襭 螳豌伎 (貊) 螳 . 一危 朱 . 覓語 覲 蟆曙磯 蟆壱 螻殊 覯譯狩(factor)襦 覲. 企ゼ 覦讌蠍 伎 I()襯 覃 .
> 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

18 襭 : read.table() #

example2.txt
	x	y
id1	1	2
id2	3	4
id3	5	NA
id4	miss	7

example3.txt
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


example2.txt 殊 曙企慨.
> 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
> 

覓語 'miss'螳 蟇碁Π. 願 missing value襦 豌襴企慨.
> 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
> 

col.names 訖襷 襴 row.names 譴 .
> 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
> 

19 一危 sql server 觜襯願 ロ蠍 #

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)
}
蠍 蠍郁鍵..
企: : るジ讓曙 襦螻豺 企Ν 譯殊語. 襦螻豺
EditText : Print : Mobile : FindPage : DeletePage : LikePages : Powered by MoniWiki : Last modified 2019-08-11 21:50:15

碁レ 蟆 螻 襷 蟆 詞朱る 螻褐 . (蟯危)