[R] Hmisc label function applied to data frame

Dieter Menne dieter.menne at menne-biomed.de
Fri Dec 3 09:40:07 CET 2010



KT_rfan wrote:
> 
> 
> I'm attempting to create a data frame with correlations between every pair
> of variables in a data frame, so that I can then sort by the value of the
> correlation coefficient and see which pairs of variables are most strongly
> correlated.
> 
> The sm2vec function in the corpcor library works very nicely as shown
> here:
> 
> library(Hmisc)
> library(corpcor)
> 
> # Create example data
> x1 = runif(50)
> x2 = runif(50)
> x3 = runif(50)
> d = data.frame(x1=x1,x2=x2,x3=x3)
> label(d$x1) = "Variable x1"
> label(d$x2) = "Variable x2"
> label(d$x3) = "Variable x3"
> .. rest of code omitted
> 

This has nothing to do with Hmisc and corpcor. I things get confusing,
simplify and use str(). 

What you wanted to "label" the columns is "names", or, probably better
named, "colnames". Note that your way of labeling converts the column to a
Class "labelled", which is not what function take for breakfeast.


d = data.frame(x1=runif(10),x2=runif(10))
label(d)  # This alone gives the error message
# x1 x2 
# "" "" "
# Warning message:
#In mapply(FUN = label, x = x, default = default, MoreArgs = list(self =
TRUE),  :
#  longer argument not a multiple of length of shorter
str(d)
# data.frame':   10 obs. of  2 variables:
# $ x1: num  0.1353 0.7234 0.0266 0.074 0.2391 ...
# $ x2: num  0.833 0.573 0.136 0.395 0.308 ...
label(d$x1) = "Variable x1"
str(d)

#'data.frame':   10 obs. of  2 variables:
# $ x1:Class 'labelled'  atomic [1:10] 0.1353 0.7234 0.0266 0.074 0.2391 ...
#  .. ..- attr(*, "label")= chr "Variable x1"
# $ x2: num  0.833 0.573 0.136 0.395 0.308 ...


# Labeling columns, the correct way
d = data.frame(x1=runif(10),x2=runif(10))
str(d)
names(d) = c("Var1","Var2")
str(d)




-- 
View this message in context: http://r.789695.n4.nabble.com/Hmisc-label-function-applied-to-data-frame-tp3069784p3070777.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list