如何通知 R 我的数据集的第一列是行名?以及如何将数据框的class变为向量或矩阵?

How to inform R that the first column of my dataset is row names? And how should change the class of data frame to vector or matrice?

   APD<- read.csv("APD.csv",header=FALSE)
  rar<- read.csv("rar.csv",header=FALSE)
 ## Making column name labels
 tree<-rep("tree",100)
 tree_labels<-c(1:100)

colnames(APD)<-c(paste(tree,tree_labels, sep="")) 
 colnames(rar)<-c(paste(tree,tree_labels, sep=""))

  correlation.csv<- cor(x=APD, y=rar, method = "spearman")

上面的脚本假设两个数据集的列之间的计算相关性。但是有两个问题它开始标记第一列的输出(这是行名)所以最后一个分数得到 NA 作为标签。我不确定我的想法是否正确,但可能出于同样的原因,R 认为 APD 是一个数据框,因此不计算最后一行。

干杯

csv 文件的子集

   V1              V2                V3    V4
t1  9.368703877 9.693286792 12.44129352 13.06908296
t10 8.128921254 8.940227268 11.40226603 12.17704779
t11 7.87062995  8.697508965 11.39250803 12.17704779

看完之后是这样的

    V2           V3            V4            V5
    V1          V2               V3         V4
 t1 9.368703877 9.693286792 12.44129352 13.06908296
t1  08.128921254    8.940227268 11.40226603 12.17704779
t11 7.87062995  8.697508965 11.39250803 12.17704779

第一个问题可以用row.names:

APD <- read.csv("APD.csv", header = FALSE, row.names = 1)
rar <- read.csv("rar.csv", header = FALSE, row.names = 1)

来自documentation

row.names: a vector of row names. This can be a vector giving the actual row names, or a single number giving the column of the table which contains the row names, or character string giving the name of the table column containing the row names.

第二个问题,可以用mapply:

mapply(cor, APD, rar, MoreArgs = list(method = "spearman"))

假设您想要每个 table 的第一列之间的相关性,然后是第二列,依此类推。


使用你的例子:

> str(a)
'data.frame':   3 obs. of  4 variables:
 $ V1: num  9.37 8.13 7.87
 $ V2: num  9.69 8.94 8.7
 $ V3: num  12.4 11.4 11.4
 $ V4: num  13.1 12.2 12.2
> mapply(cor, a, -a, MoreArgs = list(method = "spearman"))
V1 V2 V3 V4 
-1 -1 -1 -1