xml - How to get a mean from data.frame? -
i have data.frame, got this: data <- ldply(xmltolist("http://www.nbp.pl/kursy/xml/a025z100205.xml"),data.frame)
i create list :
list <- data[[6]]
then deleted na values
list <- list[!is.na(list)]
and got this
[1] 0,0900 2,9915 2,5851 0,3850 2,7805 2,0566 2,1043 4,0921 1,4918 2,7837 [11] 4,7009 0,3723 3,3450 0,1561 0,5496 0,2615 2,3211 0,4987 0,4005 0,5589 [21] 0,9894 2,0923 1,9688 1,1851 5,7733 0,0643 0,2271 0,3884 1,5965 0,8687 [31] 0,0981 3,1673 0,2557 0,4384 4,5657 35 levels: 0,0900 2,9915 2,5851 0,3850 2,7805 2,0566 2,1043 4,0921 ... 4,5657
but can't mean list. because of levels? how can this?
the class
of 'list' factor
due non-numeric component (,
). when there non-numeric element in column, creating data.frame
, default option stringsasfactors=true
. can include argument stringsasfactors=false
inside data.frame
character column, still ,
should replaced. assuming meant decimal, replace .
using sub
, convert numeric
as.numeric
, mean
.
mean(as.numeric(sub(',', '.', list)))
edit
after reading data using op's code, understand 4th , 6th columns numeric (but columns 'factor' class). if interested in 6th column, above, if need mean
of 4th , 6th, use sapply
sapply(data[c(4,6)], function(x) mean(as.numeric(sub(',', '.', x)), na.rm=true)) #przelicznik kurs_sredni #298.00000 1.66298
or
m1 <- `dim<-`(as.numeric(sub(',', '.', as.matrix(data[c(4,6)]))), dim(data[c(4,6)])) colmeans(m1, na.rm=true) #[1] 298.00000 1.66298
note: better not name objects function names.
Comments
Post a Comment