list-apply及符号函数的使用

2018/12/02

Categories: R Tags: data.frame

删除list中的部分元素

直接指定要删除的元素为NULL

lts = list(1, 2, 'a', matrix(1:3))
print(length(lts))
## [1] 4
lts[[3]]
## [1] "a"
lts[[3]] = NULL
lts[[3]]
##      [,1]
## [1,]    1
## [2,]    2
## [3,]    3
print(length(lts))
## [1] 3

按照序号/list的name删除,即按照序号或name选择要提取的元素

lts = list(1, 2, 'a', matrix(1:3))
print(length(lts))
## [1] 4
lts[c(2,4)] = NULL
print(lts)
## [[1]]
## [1] 1
## 
## [[2]]
## [1] "a"

R中操作符皆为函数

参考 Every operation is a function call

具体示例:

`+`(3,2)
## [1] 5
x = 1:10
`[`(x, 2)
## [1] 2
`[`(x, -2)
## [1]  1  3  4  5  6  7  8  9 10
`[<-`(iris[1:2, ], 'new_col_name', value = "future")
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species new_col_name
## 1          5.1         3.5          1.4         0.2  setosa       future
## 2          4.9         3.0          1.4         0.2  setosa       future

apply类函数中使用符号函数

lapply提取元素指定的列

当list中的元素为data.frame/matrix,需要提取其中的指定列

lts = split(subset(iris, select = -Species), iris$Species)
str(lts)
## List of 3
##  $ setosa    :'data.frame':  50 obs. of  4 variables:
##   ..$ Sepal.Length: num [1:50] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
##   ..$ Sepal.Width : num [1:50] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
##   ..$ Petal.Length: num [1:50] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
##   ..$ Petal.Width : num [1:50] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
##  $ versicolor:'data.frame':  50 obs. of  4 variables:
##   ..$ Sepal.Length: num [1:50] 7 6.4 6.9 5.5 6.5 5.7 6.3 4.9 6.6 5.2 ...
##   ..$ Sepal.Width : num [1:50] 3.2 3.2 3.1 2.3 2.8 2.8 3.3 2.4 2.9 2.7 ...
##   ..$ Petal.Length: num [1:50] 4.7 4.5 4.9 4 4.6 4.5 4.7 3.3 4.6 3.9 ...
##   ..$ Petal.Width : num [1:50] 1.4 1.5 1.5 1.3 1.5 1.3 1.6 1 1.3 1.4 ...
##  $ virginica :'data.frame':  50 obs. of  4 variables:
##   ..$ Sepal.Length: num [1:50] 6.3 5.8 7.1 6.3 6.5 7.6 4.9 7.3 6.7 7.2 ...
##   ..$ Sepal.Width : num [1:50] 3.3 2.7 3 2.9 3 3 2.5 2.9 2.5 3.6 ...
##   ..$ Petal.Length: num [1:50] 6 5.1 5.9 5.6 5.8 6.6 4.5 6.3 5.8 6.1 ...
##   ..$ Petal.Width : num [1:50] 2.5 1.9 2.1 1.8 2.2 2.1 1.7 1.8 1.8 2.5 ...
# 提取每个list中的1/2列
lts_sel <- lapply(lts, "[", 1:2)
str(lts_sel)
## List of 3
##  $ setosa    :'data.frame':  50 obs. of  2 variables:
##   ..$ Sepal.Length: num [1:50] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
##   ..$ Sepal.Width : num [1:50] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
##  $ versicolor:'data.frame':  50 obs. of  2 variables:
##   ..$ Sepal.Length: num [1:50] 7 6.4 6.9 5.5 6.5 5.7 6.3 4.9 6.6 5.2 ...
##   ..$ Sepal.Width : num [1:50] 3.2 3.2 3.1 2.3 2.8 2.8 3.3 2.4 2.9 2.7 ...
##  $ virginica :'data.frame':  50 obs. of  2 variables:
##   ..$ Sepal.Length: num [1:50] 6.3 5.8 7.1 6.3 6.5 7.6 4.9 7.3 6.7 7.2 ...
##   ..$ Sepal.Width : num [1:50] 3.3 2.7 3 2.9 3 3 2.5 2.9 2.5 3.6 ...
# 提取每个list中的1/2行
lts_sel <- lapply(lts, "[", 1:2, )
lts_sel
## $setosa
##   Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1          5.1         3.5          1.4         0.2
## 2          4.9         3.0          1.4         0.2
## 
## $versicolor
##    Sepal.Length Sepal.Width Petal.Length Petal.Width
## 51          7.0         3.2          4.7         1.4
## 52          6.4         3.2          4.5         1.5
## 
## $virginica
##     Sepal.Length Sepal.Width Petal.Length Petal.Width
## 101          6.3         3.3          6.0         2.5
## 102          5.8         2.7          5.1         1.9
# 提取每个list中的1:2行/1:2列
lts_sel <- lapply(lts, "[", 1:2, 1:2)
lts_sel
## $setosa
##   Sepal.Length Sepal.Width
## 1          5.1         3.5
## 2          4.9         3.0
## 
## $versicolor
##    Sepal.Length Sepal.Width
## 51          7.0         3.2
## 52          6.4         3.2
## 
## $virginica
##     Sepal.Length Sepal.Width
## 101          6.3         3.3
## 102          5.8         2.7
# 提取每个list中的1:2行/1列
lts_sel <- lapply(lts, "[", 1:2, 1, drop = F)
lts_sel
## $setosa
##   Sepal.Length
## 1          5.1
## 2          4.9
## 
## $versicolor
##    Sepal.Length
## 51          7.0
## 52          6.4
## 
## $virginica
##     Sepal.Length
## 101          6.3
## 102          5.8

mapply给list添加多余列

lts = split(subset(iris, select = -Species), iris$Species)
lts <- lapply(lts, "[", 1:2, 1:2)
lts
## $setosa
##   Sepal.Length Sepal.Width
## 1          5.1         3.5
## 2          4.9         3.0
## 
## $versicolor
##    Sepal.Length Sepal.Width
## 51          7.0         3.2
## 52          6.4         3.2
## 
## $virginica
##     Sepal.Length Sepal.Width
## 101          6.3         3.3
## 102          5.8         2.7
lts_cols = mapply(`[<-`, lts, 'Species', value = names(lts), SIMPLIFY = FALSE)
lts_cols
## $setosa
##   Sepal.Length Sepal.Width Species
## 1          5.1         3.5  setosa
## 2          4.9         3.0  setosa
## 
## $versicolor
##    Sepal.Length Sepal.Width    Species
## 51          7.0         3.2 versicolor
## 52          6.4         3.2 versicolor
## 
## $virginica
##     Sepal.Length Sepal.Width   Species
## 101          6.3         3.3 virginica
## 102          5.8         2.7 virginica
lts_cols = mapply(`[<-`, lts, paste0('Species', 1:3), value = names(lts), SIMPLIFY = FALSE)
lts_cols
## $setosa
##   Sepal.Length Sepal.Width Species1
## 1          5.1         3.5   setosa
## 2          4.9         3.0   setosa
## 
## $versicolor
##    Sepal.Length Sepal.Width   Species2
## 51          7.0         3.2 versicolor
## 52          6.4         3.2 versicolor
## 
## $virginica
##     Sepal.Length Sepal.Width  Species3
## 101          6.3         3.3 virginica
## 102          5.8         2.7 virginica

list合并为dataframe

lts_cols = mapply(`[<-`, lts, 'Species', value = names(lts), SIMPLIFY = FALSE)
do.call("rbind", lts_cols)
##               Sepal.Length Sepal.Width    Species
## setosa.1               5.1         3.5     setosa
## setosa.2               4.9         3.0     setosa
## versicolor.51          7.0         3.2 versicolor
## versicolor.52          6.4         3.2 versicolor
## virginica.101          6.3         3.3  virginica
## virginica.102          5.8         2.7  virginica
lts_cols = mapply(`[<-`, lts, paste0('Species', 1:3), value = names(lts), SIMPLIFY = FALSE)
dplyr::bind_rows(lts_cols)
##   Sepal.Length Sepal.Width Species1   Species2  Species3
## 1          5.1         3.5   setosa       <NA>      <NA>
## 2          4.9         3.0   setosa       <NA>      <NA>
## 3          7.0         3.2     <NA> versicolor      <NA>
## 4          6.4         3.2     <NA> versicolor      <NA>
## 5          6.3         3.3     <NA>       <NA> virginica
## 6          5.8         2.7     <NA>       <NA> virginica