[英]List-columns in tibbles: Can I link a list-column with another list-column?
This is my first post, so please excuse me if I sound silly or the answer I am looking for already exists. 这是我的第一篇文章,所以如果我听起来很傻或者我正在寻找的答案已经存在,请原谅。
My main problem is this: I have created a tibble containing 4 columns (a character column, two data columns and a column containing a distance matrix for each of the levels of the character column) and I am trying to create a function that uses the distance matrices from the 4th column as a dependent variable and some independent variables from the second column. 我的主要问题是:我创建了一个包含4列的小标题(一个字符列,两个数据列和一个包含字符列每个级别的距离矩阵的列),并且我试图创建一个使用第四列的距离矩阵作为因变量,第二列的距离矩阵作为一些自变量。 The problem is that R keeps warning me that it cannot find the dependent variable. 问题是R一直警告我找不到找不到因变量。
The packages I've used are the following: 我使用的软件包如下:
library(easypackages)
libraries('tidyverse', 'broom')
The tibble containing my IVs looks like this: 包含我的IV的小标题如下:
IVs_tibble
# A tibble: 175 × 8
Site Region IV.1 IV.2 IV.3 IV.4 IV.5 IV.6
<chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Site.1 A 387 169 460 234 137 445
2 Site.2 A 197 172 449 192 141 422
3 Site.3 A 86 179 432 78 147 398
4 Site.4 A 14 183 404 4 152 375
5 Site.5 B 86 179 407 80 148 382
6 Site.6 B 18 175 422 154 146 397
7 Site.7 C 132 172 429 211 142 413
8 Site.8 C 99 178 404 120 147 385
9 Site.9 D 73 177 409 150 146 382
10 Site.10 D 77 175 417 182 145 383
# ... with 165 more rows
I then nest it: 然后我将其嵌套:
by_region <- IVs_tibble %>% group_by(Region) %>% nest()
And here's how it looks: 外观如下:
by_region
# A tibble: 6 × 2
Region data
<chr> <list>
1 A <tibble [60]>
2 B <tibble [84]>
3 C <tibble [10]>
4 D <tibble [6]>
5 E <tibble [13]>
6 F <tibble [2]>
Subsequently, I create another tibble containing raw presence/absence data: 随后,我创建另一个包含原始存在/不存在数据的小标题:
regions
# A tibble: 175 × 984
Region Site Taxon.1 Taxon.2 Taxon.3
<chr> <chr> <dbl> <dbl> <dbl>
1 A Site.1 1 1 0
2 A Site.1 0 1 0
3 B Site.1 1 1 1
4 B Site.1 0 0 0
5 C Site.1 1 0 1
6 C Site.1 0 0 1
7 D Site.1 1 0 0
8 D Site.1 1 1 0
9 D Site.1 0 0 0
10 F Site.10 0 1 0
# ... with 165 more rows, and 982 more variables: (these contain taxa names)
Then I nest that tibble too: 然后我也嵌套了这个小词:
rg <- regions %>% group_by(Region) %>% nest()
And it looks like: 它看起来像:
rg
# A tibble: 6 × 2
Region IVs
<chr> <list>
1 A <tibble [60]>
2 B <tibble [84]>
3 C <tibble [10]>
4 D <tibble [6]>
5 E <tibble [13]>
6 F <tibble [2]>
And I rename the data column in order to join it with the tibble containing the IVs: 我将数据列重命名,以便与包含IV的标题结合起来:
rr <- rg %>% rename(Communities = data)
rr
# A tibble: 6 × 2
Region Communities
<chr> <list>
1 A <tibble [60]>
2 B <tibble [84]>
3 C <tibble [10]>
4 D <tibble [6]>
5 E <tibble [13]>
6 F <tibble [2]>
As a following step, I construct a function to compute the matrices: 下一步,我构造一个函数来计算矩阵:
betamatrices <-function(df){vegan::betadiver(df, method='sim')}
rr <- rr %>% mutate(model = map(data,betamatrices))
The rr tibble now looks like this: rr小标题现在看起来像这样:
rr
# A tibble: 6 × 3
Region Communities Dist.matrix
<chr> <list> <list>
1 A <tibble [60]> <S3: dist>
2 B <tibble [84]> <S3: dist>
3 C <tibble [10]> <S3: dist>
4 D <tibble [6]> <S3: dist>
5 E <tibble [13]> <S3: dist>
6 F <tibble [2]> <S3: dist>
And then, I join the two tibbles: 然后,我加入了两个小贴士:
my_tibble <- by_region %>% left_join(rr)
The tibble looks like this: 小标题如下所示:
my_tibble
# A tibble: 6 × 4
Region IVs Communities Dist.matrix
<chr> <list> <list> <list>
1 A <tibble [60]> <tibble [60]> <S3: dist>
2 B <tibble [84]> <tibble [84]> <S3: dist>
3 C <tibble [10]> <tibble [10]> <S3: dist>
4 D <tibble [6]> <tibble [6]> <S3: dist>
5 E <tibble [13]> <tibble [13]> <S3: dist>
6 F <tibble [2]> <tibble [2]> <S3: dist>
And the function I want to apply looks like this: 我要应用的功能如下所示:
mrm_model <- function(df){ecodist::MRM(Dist.matrix~dist(IV.1) + dist(IV.2),data = (df))}
When I try to compute it with the following code: 当我尝试使用以下代码进行计算时:
my_tibble <- my_tibble %>% mutate(mrm = map(IVs,mrm_model))
, my_tibble <- my_tibble %>% mutate(mrm = map(IVs,mrm_model))
,
I get this error message: 我收到此错误消息:
Error in mutate_impl(.data, dots) : object 'Dist.matrix' not found
. Error in mutate_impl(.data, dots) : object 'Dist.matrix' not found
。
Do you have any idea why this keeps popping up? 您知道为什么它会不断弹出吗?
When I try to "correct" the function with the $ sign: 当我尝试使用$符号“更正”该函数时:
mrm_model <- function(df){ecodist::MRM(my_tibble$Dist.matrix~dist(Area),data = (df))}
, mrm_model <- function(df){ecodist::MRM(my_tibble$Dist.matrix~dist(Area),data = (df))}
,
I get the following warning: 我收到以下警告:
Error in mutate_impl(.data, dots) : invalid type (list) for variable 'my_tibble$Dist.matrix'
. Error in mutate_impl(.data, dots) : invalid type (list) for variable 'my_tibble$Dist.matrix'
。
I am an absolute newbie in this type of data-manipulation, so obviously I am over my head and I would greatly appreciate all the help I can get. 在这种数据处理方面,我绝对是新手,因此显然我不知所措,我将非常感谢我能获得的所有帮助。
I figured out that the problem can be solved if the tibble contains BOTH the presence/absence data and the IVs. 我发现,如果小标题同时包含存在/不存在数据和IV,则可以解决该问题。 Anyway, thanks for the interest lukeA 无论如何,感谢您对lukeA的关注
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.