Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用成对数据创建数据集并将其转换为矩阵_R_Matrix_Dataframe_Missing Data - Fatal编程技术网

R 使用成对数据创建数据集并将其转换为矩阵

R 使用成对数据创建数据集并将其转换为矩阵,r,matrix,dataframe,missing-data,R,Matrix,Dataframe,Missing Data,所以,我使用R尝试在一个数据集上做一个系统发育PCA,我使用了phytools包中的phyl.PCA函数。但是,我在以函数可以接受的方式组织数据时遇到了问题!这还不是全部:我做了一些实验,我知道还有更多的问题要解决,我将深入探讨 直截了当地说,下面是我正在使用的数据帧(带有虚拟数据): >all Taxa Tibia Feather 1 Microraptor 138 101 2 Microraptor 139

所以,我使用R尝试在一个数据集上做一个系统发育PCA,我使用了phytools包中的phyl.PCA函数。但是,我在以函数可以接受的方式组织数据时遇到了问题!这还不是全部:我做了一些实验,我知道还有更多的问题要解决,我将深入探讨

直截了当地说,下面是我正在使用的数据帧(带有虚拟数据):

>all
                  Taxa Tibia Feather
1          Microraptor 138   101
2          Microraptor 139   114
3          Microraptor 145   141
4           Anchiornis 160   81
5           Anchiornis 14    NA
6        Archaeopteryx 134   82
7        Archaeopteryx 136   71
8        Archaeopteryx 132   NA
9        Archaeopteryx 14    NA
10 Scansoriopterygidae 120   85
11 Scansoriopterygidae 116   NA
12 Scansoriopterygidae 123   NA
13           Sapeornis 108   NA
14           Sapeornis 112   86
15           Sapeornis 118   NA
16           Sapeornis 103   NA
17      Confuciusornis 96    NA
18      Confuciusornis 107   30
19      Confuciusornis 148   33
20      Confuciusornis 128   61
分类群被排列成一棵树(称为“树”),小猛禽是最基本的,然后依次发展到孔子鸟:

>summary(tree)

Phylogenetic tree: tree 

  Number of tips: 6 
  Number of nodes: 5 
  Branch lengths:
    mean: 1 
    variance: 0 
    distribution summary:
   Min. 1st Qu.  Median 3rd Qu.    Max. 
      1       1       1       1       1 
  No root edge.
  Tip labels: Confuciusornis 
              Sapeornis
              Scansoriopterygidae
              Archaeopteryx
              Anchiornis
              Microraptor
  No node labels.
以及功能:

>phyl.pca(tree, all, method="BM", mode="corr")
> all_agg <- aggregate(all[,-1],by=list(all$Taxa),mean,na.rm=TRUE)
> phyl.pca(tree, all_agg_matrix, method = "BM", mode = "corr")
[1] "Y has no names. function will assume that the row order of Y matches tree$tip.label"
Error in invC %*% X : requires numeric/complex matrix/vector arguments
这就是即将出现的错误:

Error in phyl.pca(tree, all, method = "BM", mode = "corr") : 
number of rows in Y cannot be greater than number of taxa in your tree
Y是“所有”数据帧。因此,我的树中有6个分类群(与数据框中的6个分类群相匹配),但数据框中有20行。所以我用了这个函数:

>phyl.pca(tree, all, method="BM", mode="corr")
> all_agg <- aggregate(all[,-1],by=list(all$Taxa),mean,na.rm=TRUE)
> phyl.pca(tree, all_agg_matrix, method = "BM", mode = "corr")
[1] "Y has no names. function will assume that the row order of Y matches tree$tip.label"
Error in invC %*% X : requires numeric/complex matrix/vector arguments
分类群的顺序发生了变化,这有点奇怪。。。这样行吗

在任何情况下,我将其转换为矩阵:

> all_agg_matrix <- as.matrix(all_agg)
> all_agg_matrix
                 Group.1   Tibia Feather
[1,]          "Anchiornis" "153"    "81"
[2,]       "Archaeopteryx" "136"    "77"
[3,]      "Confuciusornis" "120"    "41"
[4,]         "Microraptor" "141"   "119"
[5,]           "Sapeornis" "110"    "86"
[6,] "Scansoriopterygidae" "120"    "85"

所以,现在函数考虑分类单元的顺序是完全错误的(但我可以相对容易地解决这个问题)。问题是phyl.pca似乎不相信我的矩阵实际上是一个矩阵。你知道为什么吗?

我想你可能有更大的问题。我猜想,包括
phyl.pca
在内的大多数系统发育方法都假设性状在物种水平上是固定的(即,它们不考虑物种内的变异)。因此,如果要使用
phyl.pca
,可能需要将数据压缩为每个物种的单个值,例如通过

dd_agg <- aggregate(dd[,-1],by=list(dd$Taxa),mean,na.rm=TRUE)
使用这些聚合数据,我可以构建一个树(因为您没有给我们一个树),然后运行
phyl.pca

library(phytools)
tt <- rcoal(nrow(dd_agg),tip.label=dd_agg[,1])
phyl.pca(tt,dd_mat)
库(植物醇)

Ben Bolker给出的答案似乎有效,即在创建矩阵和运行函数之前,数据(称为“所有”)被压缩为每个物种的单个值。根据so:

> all_agg <- aggregate(all[,-1],by=list(all$Taxa),mean,na.rm=TRUE)
> all_mat <- all_agg[,-1]
> rownames(all_mat) <- all_agg[,1]
> phyl.pca(tree,all_mat, method= "lambda", mode = "corr")
>all_agg all_mat rownames(all_mat)phyl.pca(树,all_mat,method=“lambda”,mode=“corr”)

感谢所有提供答案的人,尤其是本!:)

我认为第一条错误信息相当清楚。。。你用什么做一棵树,你能把它贴出来吗?代码是,它只是检查Y的行数与Ntip(树)。至于问题的第二部分,你的矩阵应该只有数字是,而不是像
138、101这样的成对数字。NAs和空值可能是另一个问题,但您需要首先提供一个矩阵。您好,我已经发布了树的详细信息。这是一个非常简单的过程,从小猛禽到孔子龙。至于矩阵,如果不是成对的,我如何确保每个“胫骨”编号与相应的“羽毛”编号相对应?是否有其他解决方案?请参阅我对答案的编辑。不要在整个数据帧上使用
作为.matrix
;它正在将您的数字列转换为字符串…感谢您的评论!根据您的建议所做的更改和新发行的内容,请参阅原始帖子。。。