R - Pie, X values must be positive - r

I am new to R and drew some testdata about countries in a csv from the web. I am currenty fooling arround with plotting and encountered said error while creating a pie chart of the worlds unemployment.
i issued the following:
>values <- read.csv("D:\\test\\countrydata.csv")
>names(values)
[1] "name" "size" "pop" "unemployed" ...
>typeof(values$unemployed)
"integer"
>pie(values$pop)
Error in pie(values$unemployed) :
'x' values must be positive
>pie(values$pop, na.rm=TRUE)
Error in pie(values$unemployed, na.rm=TRUE) :
'x' values must be positive
The dataset i want to plot is a set of integers, all of them are positive, 0 (thanks kim) or NA.
0 are not a problem when plotting integers, i tried
>pie(as.integer(c(0,1,2,3))
and it worked fine.
What am i missing here?
Thanks and Regards,
BillDoor

I don't have access to your data but in my experience the following might help and is definitely worth a try:
pie(table(values$unemployed))
Would love to learn whether this solved your problem!

According to the R documentation, the first argument must be "non-negative vector" Now when we write
pie(value$pop)
Then R considers it as a numeric data frame which you can check by
str(value$pop).
When you typecast via as.numeric or as.integer, you convert it to a vector, that's why it works on typecasting:
pie(as.numeric(value$pop))

I encountered a similar problem. Here's what worked for me:
vectorVal <- as.numeric(table)
pie(vectorVal)

Related

PCA in R: Error in svd(x, nu=0, nv=k) : Infinite or missing values in 'x'

My dataframe contains about 26k rows with 129 variables. I've made sure all of the variables are numeric and do not have any NA values (used na.omit). Using the function prcomp() on my dataframe tells me "Infinite or missing values in x". What might I be overlooking then?
Did you also make sure none of them are infinite? As that's the other part of that message?
Easily check all this with:
all( is.finite( your.data.frame ) )

What does "argument to 'which' is not logical" mean in FactoMineR MCA?

I'm trying to run an MCA on a datatable using FactoMineR. It contains only 0/1 numerical columns, and its size is 200.000 * 20.
require(FactoMineR)
result <- MCA(data[, colnames, with=F], ncp = 3)
I get the following error :
Error in which(unlist(lapply(listModa, is.numeric))) :
argument to 'which' is not logical
I didn't really know what to do with this error. Then I tried to turn every column to character, and everything worked. I thought it could be useful to someone else, and that maybe someone would be able to explain the error to me ;)
Cheers
Are the classes of your variables character or factor?I was having this problem. My solution was to change al variables to factor.
#my data.frame was "aux.da"
i=0
while(i < ncol(aux.da)){
i=i+1 aux.da[,i] = as.factor(aux.da[,i])
}
It's difficult to tell without further input, but what you can do is:
Find the function where the error occurred (via traceback()),
Set a breakpoint and debug it:
trace(tab.disjonctif, browser)
I did the following (offline) to find the name of tab.disjonctif:
Found the package on the CRAN mirror on GitHub
Search for that particular expression that gives the error
I just started to learn R yesterday, but the error comes from the fact that the MCA is for categorical data, so that's why your data cannot be numeric. Then to be more precise, before the MCA a "tableau disjonctif" (sorry i don't know the word in english : Complete disjunctive matrix) is created.
So FactomineR is using this function :
https://github.com/cran/FactoMineR/blob/master/R/tab.disjonctif.R
Where i think it's looking for categorical values that can be matched to a numerical value (like Y = 1, N = 0).
For others ; be careful : for R categorical data is related to factor type, so even if you have characters you could get this error.
To build off #marques, #Khaled, and #Pierre Gourseaud:
Yes, changing the format of your variables to factor should address the error message, but you shouldn't change the format of numerical data to factor if it's supposed to be continuous numerical data. Rather, if you have both continuous and categorical variables, try running a Factor Analysis for Mixed Data (FAMD) in the same FactoMineR package.
If you go the FAMD route, you can change the format of just your categorical variable columns to factor with this:
data[,c(3:5,10)] <- lapply(data[,c(3:5,10)] , factor)
(assuming column numbers 3,4,5 and 10 need to be changed).
This will not work for only numeric variables. If you only have numeric use PCA. Otherwise, add a factor variable to your data frame. It seems like for your case you need to change your variables to binary factors.
Same problem as well and changing to factor did not solve my answer either, because I had put every variable as supplementary.
What I did first was transform all my numeric data to factor :
Xfac = factor(X[,1], ordered = TRUE)
for (i in 2:29){
tfac = factor(X[,i], ordered = TRUE)
Xfac = data.frame(Xfac, tfac)
}
colnames(Xfac)=labels(X[1,])
Still, it would not work. But my 2nd problem was that I included EVERY factor as supplementary variable !
So these :
MCA(Xfac, quanti.sup = c(1:29), graph=TRUE)
MCA(Xfac, quali.sup = c(1:29), graph=TRUE)
Would generate the same error, but this one works :
MCA(Xfac, graph=TRUE)
Not transforming the data to factors also generated the problem.
I posted the same answer to a related topic : https://stackoverflow.com/a/40737335/7193352

R programming MCA() in FactoMineR error message

I was using the MCA() function from FactoMineR package in R to do the multiple correspondence analysis on a set of around 160 variables with around 2000 observations. Around 150 of the variables are continuous, so I first used the cut() function to convert those continuous variables to categorical variables and then used MCA() function.
My code is very simple like this:
library(FactoMineR)
data<-read.csv('demographics.csv')
for (i in 9:length(data)){
temp<-unlist(data[i],use.names=FALSE)
data[i]<-cut(temp,breaks=5,labels=c('A','B','C','D','E'))
}
MC<-MCA(data,ncp=10,graph=TRUE)
After I run the code, I got the following error message.
Error in dimnames(res) <- list(attributes(tab)$row.names, listModa) : length of 'dimnames' [2] not equal to array extent
I am wondering why this error occurs and how to fix it. There is no missing data in my table and all of the variables are categorical.
If anyone has encountered similar problems and would love to help, I would really appreciate it. Thanks a lot.
I have had this error before because the function requires the variables to be factors (and the data I was passing it wasn't fully converted into factors). Unlike a lot of other R functions, this one does not convert the data for you even if all columns are categorical.
I'm not quite sure what your data is, but it is likely that one or more columns is not as a factor variable. If your columns 1 to 8 are already factors then it may be in the read.csv call; string variables will automatically be converted into a factors when you read them in from the csv, but numeric ones will not.

mean.default argument not numerical on R

I'm trying to use R for the first time, I have never taken courses and have some questions. the first is this:
when I try to do the mean of some Temperature values (they are all between 18.15 and 18.40)
I get this answer
"Warning message:
In mean.default(d_Temp_Experiment$value) :
argument is not numeric or logical: returning NA"
I dont' have the same problem with values of PAR 5that are all integer numbers and with values of pH all decimal numbers like 8.831...
Can you tell what I do wrong?
As Arun hints at it could be that the column is character rather than numeric.
If you are sure that all the values are correct you could coerce the values with
d_Temp_Experiment$value <- as.numeric(d_Temp_Experiment$value)
You might have the below sort of business going on.
myvector <- c(0,1,2,3,4,5,"6","7")
mv <- as.numeric(myvector)
mean(myvector)
mean(mv)

Error in hist.default(xa) : 'x' must be numeric

I'm a complete R beginner, and am trying to do something pretty basic - make histograms of two vectors I imported from Excel.
The vectors are xa and xb. I tried hist(xa), and get the following error:
Error in hist.default(xa) : 'x' must be numeric
So I did some searching, and tried to remedy this using as.numeric(xa), and got:
Error: (list) object cannot be coerced to type 'double'
So I tried the as.list function, but it turned my vector into a matrix. Not really sure what's going on. The numbers in the vectors are all 4 digits between about -2 and +10. Any help would be greatly appreciated!
Here's something you can try... no guarantees, since you have not given a working example:
newXa <- sapply(xa, as.numeric)
hist(newXa)
What should be done is to look at the structure of 'x'
str(x)
Then if 'xa' is how you are referring to x[['a']] you would do this:
hist( x[['a']] )
And if str(x) showed that the "a" column were a factor, one might have more success with this:
hist( as.numeric(as.character(x[['a']])) )

Resources