سؤال

Today I tried to finish my last R-study-exercises, but I failed. I`m not allowed to you show the precise instructions of my academic, but you may can help me with the warnings I get after sourcing. What do they mean? What doesn't fit? I know, this question is very vague, but this is the only way to ask. I believe its about "Aufgabe 3" and "Aufgabe 4"

This is my input:

x <- read.csv ("http://hci.stanford.edu/jheer/workshop/data/worldbank/worldbank.csv")
y <- (colnames (x) <- (c ("Country", "Year", "co2", "power","energy", "fertility", "gni", "internet", "life.expectancy","military", "population", "hiv.prevalence")))
y

####Aufgabe 1####

f1 <- min(x$fertility, na.rm=TRUE)
f1

####Aufgabe 2####

f2 <- max (subset(x, Country=="Australia" | Country=="Belarus" | Country=="Germany", select=fertility), na.rm=TRUE)
f2


####Aufgabe 3####

q1 <- quantile (subset(x, Year==2005, select=military), probs=c(.25), na.rm=TRUE)
q1

####Aufgabe 4####

q2 <- quantile (subset(x, Year==2001, select=population), probs=c(.05), na.rm=TRUE)
q2


####Aufgabe 4####
n <- length(which (is.na (subset (x, Year==2000, select=military))))
n

####Aufgabe 5####


library(psych)

mil<- skew (x$military)
coun<- skew(x$Country)
Ye<- skew(x$Year)
co<- skew(x$co2)
po<- skew(x$power)
en<- skew(x$energy)
fer<- skew(x$fertility)
gni<- skew(x$gni)
inertnet<- skew(x$internet)
life<- skew(x$life.expectancy)
pop<- skew(x$population)
hiv<- skew(x$hiv.prevalence)

mil
coun
Ye
co
po
en
fer
gni
inertnet
life
pop
hiv

ku1<- "co2"
ku1

...and these warnings I get after sourcing:

1. In var(as.vector(x), na.rm = na.rm : Na generated through conversion
2. n mean.default(x) : argument is not numeric or logical: returning NA
3. 3: In Ops.factor(x, mx) : - not meaningful for factors
4. In var(as.vector(x), na.rm =na.rm) : Na generated through conversion 
هل كانت مفيدة؟

المحلول

  1. Means that the as.vector(x) operation resulted in one or more elements of x being converted to NA as the conversion for those components is not defined.
  2. When mean.default is called, x is neither numeric or logical and hence the function can't do anything with the data
  3. Means that x or mx or both are factors and - (and other mathematical operators) is not defined for factor objects.
  4. See 1. above.

All point to an issue with the input data, usually a factor has been created.

The warnings come from this line:

> coun <- skew(x$Country)
Warning messages:
1: In var(as.vector(x), na.rm = na.rm) : NAs introduced by coercion
2: In mean.default(x) : argument is not numeric or logical: returning NA
3: In Ops.factor(x, mx) : - not meaningful for factors
4: In var(as.vector(x), na.rm = na.rm) : NAs introduced by coercion

This is because x$Country is a factor:

> str(x)
'data.frame':   1362 obs. of  12 variables:
 $ Country        : Factor w/ 227 levels "","Afghanistan",..: 19 19 19 19 19 19 166 166 166 166 ...
....

Even if you made this a character you could compute the skewness of this variable. Just comment this line out.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top