在R中创建长度不等的数据帧列表
我目前遇到了创建长度不等列表的数据帧的问题。我的问题是,每个列表都有相应的分类数据 这就是我目前所拥有的在R中创建长度不等的数据帧列表,r,dataframe,lapply,raster,R,Dataframe,Lapply,Raster,我目前遇到了创建长度不等列表的数据帧的问题。我的问题是,每个列表都有相应的分类数据 这就是我目前所拥有的 #Extract raster values to polygons v <- extract(burnclass, ws_proj2) v #Get class counts for each polygon depending on the number of polygons v.counts <- lapply(v,table) v.counts 我尝试了下面
#Extract raster values to polygons
v <- extract(burnclass, ws_proj2)
v
#Get class counts for each polygon depending on the number of polygons
v.counts <- lapply(v,table)
v.counts
我尝试了下面的代码来创建一个数据帧,但是结果将NA值附加到每个字符串的末尾,这不是我需要的
res <- as.data.frame(do.call(rbind,lapply(v.counts, `length<-`,max(indx))))
colnames(res) <- names(v.counts[[which.max(indx)]])
res
res您可以:
levs <- unique(unlist(v))
t(sapply(v, function(x) table(factor(x, levs))))
1 2 3 4 5 6 7 -1
[1,] 17 81 210 127 83 59 172 0
[2,] 0 0 660 796 612 647 348 0
[3,] 0 0 11 55 76 98 323 0
[4,] 229 173 136 114 97 111 322 963
[5,] 0 0 664 289 192 526 1122 0
[6,] 0 0 450 386 158 125 144 0
[7,] 0 0 104 391 557 1221 885 5392
levs您可以:
levs <- unique(unlist(v))
t(sapply(v, function(x) table(factor(x, levs))))
1 2 3 4 5 6 7 -1
[1,] 17 81 210 127 83 59 172 0
[2,] 0 0 660 796 612 647 348 0
[3,] 0 0 11 55 76 98 323 0
[4,] 229 173 136 114 97 111 322 963
[5,] 0 0 664 289 192 526 1122 0
[6,] 0 0 450 386 158 125 144 0
[7,] 0 0 104 391 557 1221 885 5392
levsApurrr
solution
库(purrr)
map_-dfr(v.counts,~.x)[顺序(如数字(名称)(map_-dfr(v.counts,~.x)))]
#>#tibble:7 x 8
#> `-1` `1` `2` `3` `4` `5` `6` `7`
#>
#>1 NA 17 81 210 127 83 59 172
#>2 NA NA 660 796 612 647 348
#>3 NA NA 11 55 76 98 323
#> 4 963 229 173 136 114 97 111 322
#>5 NA NA 664 289 192 526 1122
#>6 NA NA 450 386 158 125 144
#>75392 NA 104 391 557 1221 885
由于您没有提供任何dput,我创建了v.counts
as
v.counts <- list(c(`1` = 17, `2` = 81, `3` = 210, `4` = 127, `5` = 83, `6` = 59, `7` = 172),
c(`3` = 660, `4` = 796, `5` = 612, `6` = 647, `7` = 348),
c(`3` = 11, `4` = 55, `5` = 76, `6` = 98, `7` = 323),
c(`-1` = 963, `1` =229, `2` = 173, `3` = 136, `4` = 114, `5` = 97, `6` = 111, `7` = 322),
c(`3` = 664, `4` = 289, `5` = 192, `6` = 526, `7` = 1122),
c(`3` = 450, `4` = 386, `5` = 158, `6` = 125, `7` = 144),
c(`-1` = 5392,`3` = 104, `4` = 391, `5` = 557, `6` = 1221, `7` = 885))
v.counts
#> [[1]]
#> 1 2 3 4 5 6 7
#> 17 81 210 127 83 59 172
#>
#> [[2]]
#> 3 4 5 6 7
#> 660 796 612 647 348
#>
#> [[3]]
#> 3 4 5 6 7
#> 11 55 76 98 323
#>
#> [[4]]
#> -1 1 2 3 4 5 6 7
#> 963 229 173 136 114 97 111 322
#>
#> [[5]]
#> 3 4 5 6 7
#> 664 289 192 526 1122
#>
#> [[6]]
#> 3 4 5 6 7
#> 450 386 158 125 144
#>
#> [[7]]
#> -1 3 4 5 6 7
#> 5392 104 391 557 1221 885
v.counts[[1]]
#> 1 2 3 4 5 6 7
#> 17 81 210 127 83 59 172
#>
#> [[2]]
#> 3 4 5 6 7
#> 660 796 612 647 348
#>
#> [[3]]
#> 3 4 5 6 7
#> 11 55 76 98 323
#>
#> [[4]]
#> -1 1 2 3 4 5 6 7
#> 963 229 173 136 114 97 111 322
#>
#> [[5]]
#> 3 4 5 6 7
#> 664 289 192 526 1122
#>
#> [[6]]
#> 3 4 5 6 7
#> 450 386 158 125 144
#>
#> [[7]]
#> -1 3 4 5 6 7
#> 5392 104 391 557 1221 885
由(v2.0.0)于2021年6月7日创建
库(purrr)
map_-dfr(v.counts,~.x)[顺序(如数字(名称)(map_-dfr(v.counts,~.x)))]
#>#tibble:7 x 8
#> `-1` `1` `2` `3` `4` `5` `6` `7`
#>
#>1 NA 17 81 210 127 83 59 172
#>2 NA NA 660 796 612 647 348
#>3 NA NA 11 55 76 98 323
#> 4 963 229 173 136 114 97 111 322
#>5 NA NA 664 289 192 526 1122
#>6 NA NA 450 386 158 125 144
#>75392 NA 104 391 557 1221 885
由于您没有提供任何dput,我创建了v.counts
as
v.counts <- list(c(`1` = 17, `2` = 81, `3` = 210, `4` = 127, `5` = 83, `6` = 59, `7` = 172),
c(`3` = 660, `4` = 796, `5` = 612, `6` = 647, `7` = 348),
c(`3` = 11, `4` = 55, `5` = 76, `6` = 98, `7` = 323),
c(`-1` = 963, `1` =229, `2` = 173, `3` = 136, `4` = 114, `5` = 97, `6` = 111, `7` = 322),
c(`3` = 664, `4` = 289, `5` = 192, `6` = 526, `7` = 1122),
c(`3` = 450, `4` = 386, `5` = 158, `6` = 125, `7` = 144),
c(`-1` = 5392,`3` = 104, `4` = 391, `5` = 557, `6` = 1221, `7` = 885))
v.counts
#> [[1]]
#> 1 2 3 4 5 6 7
#> 17 81 210 127 83 59 172
#>
#> [[2]]
#> 3 4 5 6 7
#> 660 796 612 647 348
#>
#> [[3]]
#> 3 4 5 6 7
#> 11 55 76 98 323
#>
#> [[4]]
#> -1 1 2 3 4 5 6 7
#> 963 229 173 136 114 97 111 322
#>
#> [[5]]
#> 3 4 5 6 7
#> 664 289 192 526 1122
#>
#> [[6]]
#> 3 4 5 6 7
#> 450 386 158 125 144
#>
#> [[7]]
#> -1 3 4 5 6 7
#> 5392 104 391 557 1221 885
v.counts[[1]]
#> 1 2 3 4 5 6 7
#> 17 81 210 127 83 59 172
#>
#> [[2]]
#> 3 4 5 6 7
#> 660 796 612 647 348
#>
#> [[3]]
#> 3 4 5 6 7
#> 11 55 76 98 323
#>
#> [[4]]
#> -1 1 2 3 4 5 6 7
#> 963 229 173 136 114 97 111 322
#>
#> [[5]]
#> 3 4 5 6 7
#> 664 289 192 526 1122
#>
#> [[6]]
#> 3 4 5 6 7
#> 450 386 158 125 144
#>
#> [[7]]
#> -1 3 4 5 6 7
#> 5392 104 391 557 1221 885
由(v2.0.0)于2021-06-07创建,这里是一个基本R选项,使用Reduce
+merge
+as.list
(感谢@AnilGoyal的数据)
这里是一个基本的R选项,使用Reduce
+merge
+as.list
(感谢@AnilGoyal的数据)
Adata.table
方法,使用AnilGoyal回答中提供的样本数据
library(data.table)
# Create a list of data.tables from the named vectors
L <- lapply(v.counts, function(x) data.table(names = names(x), x))
# rowbind list and cast to wide format
dcast(rbindlist(L, idcol = "id"), id ~ names, value.var = "x", fill = NA)
# id -1 1 2 3 4 5 6 7
# 1: 1 NA 17 81 210 127 83 59 172
# 2: 2 NA NA NA 660 796 612 647 348
# 3: 3 NA NA NA 11 55 76 98 323
# 4: 4 963 229 173 136 114 97 111 322
# 5: 5 NA NA NA 664 289 192 526 1122
# 6: 6 NA NA NA 450 386 158 125 144
# 7: 7 5392 NA NA 104 391 557 1221 885
库(data.table)
#从命名向量创建data.tables列表
LA数据。表
方法,使用AnilGoyal回答中提供的样本数据
library(data.table)
# Create a list of data.tables from the named vectors
L <- lapply(v.counts, function(x) data.table(names = names(x), x))
# rowbind list and cast to wide format
dcast(rbindlist(L, idcol = "id"), id ~ names, value.var = "x", fill = NA)
# id -1 1 2 3 4 5 6 7
# 1: 1 NA 17 81 210 127 83 59 172
# 2: 2 NA NA NA 660 796 612 647 348
# 3: 3 NA NA NA 11 55 76 98 323
# 4: 4 963 229 173 136 114 97 111 322
# 5: 5 NA NA NA 664 289 192 526 1122
# 6: 6 NA NA NA 450 386 158 125 144
# 7: 7 5392 NA NA 104 391 557 1221 885
库(data.table)
#从命名向量创建data.tables列表
L我认为您也可以使用这个简单的解决方案,使用@AnikGoyal的样本数据:
library(dplyr)
v.counts %>% bind_rows() %>% select(sort(names(.)))
# A tibble: 7 x 8
`-1` `1` `2` `3` `4` `5` `6` `7`
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 NA 17 81 210 127 83 59 172
2 NA NA NA 660 796 612 647 348
3 NA NA NA 11 55 76 98 323
4 963 229 173 136 114 97 111 322
5 NA NA NA 664 289 192 526 1122
6 NA NA NA 450 386 158 125 144
7 5392 NA NA 104 391 557 1221 885
库(dplyr)
v、 计数%>%bind_rows()%%>%select(排序(名称())
#一个tibble:7x8
`-1` `1` `2` `3` `4` `5` `6` `7`
1 NA 17 81 210 127 83 59 172
2 NA NA 660 796 612 647 348
3 NA NA 11 55 76 98 323
4 963 229 173 136 114 97 111 322
5 NA NA 664 289 192 526 1122
6 NA NA 450 386 158 125 144
75392 NA 104 391 557 1221 885
我认为您也可以使用这个简单的解决方案,使用@AnikGoyal的示例数据:
library(dplyr)
v.counts %>% bind_rows() %>% select(sort(names(.)))
# A tibble: 7 x 8
`-1` `1` `2` `3` `4` `5` `6` `7`
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 NA 17 81 210 127 83 59 172
2 NA NA NA 660 796 612 647 348
3 NA NA NA 11 55 76 98 323
4 963 229 173 136 114 97 111 322
5 NA NA NA 664 289 192 526 1122
6 NA NA NA 450 386 158 125 144
7 5392 NA NA 104 391 557 1221 885
库(dplyr)
v、 计数%>%bind_rows()%%>%select(排序(名称())
#一个tibble:7x8
`-1` `1` `2` `3` `4` `5` `6` `7`
1 NA 17 81 210 127 83 59 172
2 NA NA 660 796 612 647 348
3 NA NA 11 55 76 98 323
4 963 229 173 136 114 97 111 322
5 NA NA 664 289 192 526 1122
6 NA NA 450 386 158 125 144
75392 NA 104 391 557 1221 885
能否以可复制的格式包含样品的dput。像dput(v.counts)
一样,您可以将样本的dput包含在可复制的格式中。像dput(v.counts)
这显然不会产生OP所要求的结果。@Limey,你说它不会是什么意思?只需将0替换为NA,您就会得到相同的结果。我猜@Limey希望将0
s替换为NA
s。无论如何,你的答案已经投了赞成票!这显然没有产生OP要求的结果。@Limey,你说没有是什么意思?只需将0替换为NA,您就会得到相同的结果。我猜@Limey希望将0
s替换为NA
s。无论如何,你的答案已经投了赞成票!干得好!向上投票!干得好!向上投票!精彩的使用dcast
,向上投票。精彩的使用dcast
,向上投票。宾果!简单而优雅谢谢兄弟。简洁,向上投票!谢谢你亲爱的托马斯,我还推荐了你的绝妙解决方案。哇,太简单了。。我知道我把这项任务复杂化了。谢谢大家!!答对 了简单而优雅谢谢兄弟。简洁,向上投票!谢谢你亲爱的托马斯,我还推荐了你的绝妙解决方案。哇,太简单了。。我知道我把这项任务复杂化了。