When I build the following data.frame:
cntrydata<-as.data.frame(cbind(c('BE', 'BG', 'CH', 'CY', 'CZ', 'DE', 'DK', 'EE',
'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU', 'IE',
'IL', 'LT', 'NL', 'NO', 'PL', 'PT', 'RU', 'SE',
'SI', 'SK', 'UA'),c('C', 'P', 'C', 'P', 'P', 'C',
'C', 'C', 'C', 'C', 'C', 'C', 'P', 'P', 'P', 'C',
'P', 'P', 'C', 'C', 'P', 'C', 'P', 'C', 'P', 'P', 'P'),
c(7.1, 3.6, 8.7, 6.3, 4.6, 7.9, 9.3, 6.5,
6.1, 9.1, 6.8, 7.6, 3.5, 4.1, 4.7, 8, 6.1, 5, 8.8,
8.6, 5.3, 6, 2.1, 9.2, 6.4, 4.3, 2.4)))
colnames(cntrydata)<-c('cntry','mode','CPI')
The CPI variable is of the class(factor)
, while I need it to be numeric to make the following function to work:
boxplot(CPI~mode, data=cntrydata)
I tried the following:
as.numeric(levels(cntrydata$CPI))[cntrydata$CPI]
As adviced on How to convert a factor to an integer\numeric without a loss of information?
But it is still of the class factor. Any ideas how to reach my goal?
Also, but less importantly, I was looking how to include the colnames
argument in the data construction command (instead of afterwards, as I did eventually). But couldn't find how and where to put it?