correlation - 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")
the above script suppose 2 calculated correlation between columns of 2 data sets . there 2 problem starts labeling output first column (which row name) last score gets na label. i'm not sure if i'm thinking correctly or not maybe same reason r thinks apd data frame not calculate last line.
cheers
subset of csv file
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
after read in it's below
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
for first problem, can use row.names
:
apd <- read.csv("apd.csv", header = false, row.names = 1) rar <- read.csv("rar.csv", header = false, row.names = 1)
from documentation:
row.names
: vector of row names. can vector giving actual row names, or single number giving column of table contains row names, or character string giving name of table column containing row names.
for second problem, can use mapply
:
mapply(cor, apd, rar, moreargs = list(method = "spearman"))
assuming want correlation between 1st column of each table, 2nd column, , on.
using example:
> 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
Comments
Post a Comment