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']])) )
Related
I am trying to run a boruta feature selection on my data set.
The code is below:
df<-read.csv('F:/DataAnalyticsClub/DACaseComp/DatasetDist/Datasets/BestFile.csv',stringsAsFactors=FALSE )
install.packages("Boruta")
library(Boruta)
df[is.na(df)] <- 0
df[df == ""] <- 0
X<-df[ , -which(names(df) %in% c("PREVSALEDATE","PREVSALEDATE2","ClassLabel", "PARID", "PROPERTYUNIT", "PriceDiff1", "PriceDiff2", "DateDiff1", "DateDiff2", "SALEDATE"))]
Y<-df['ClassLabel']
factorCols <- c("SCHOOLDESC","MUNIDESC","SALEDESC","INSTRTYPDESC","NEIGHDESC","TAXDESC","TAXSUBCODE_DESC","OWNERDESC","USEDESC","LOTAREA","CLEANGREEN","FARMSTEADFLAG","ABATEMENTFLAG","COUNTYEXEMPTBLDG","STYLEDESC","EXTFINISH_DESC","ROOFDESC","BASEMENTDESC","GRADEDESC","CONDITIONDESC","CDUDESC","HEATINGCOOLINGDESC","BSMTGARAGE")
nonFactorCols<-c("PRICE","COUNTYTOTAL","LOCALTOTAL","FAIRMARKETTOTAL","STORIES","YEARBLT","TOTALROOMS","BEDROOMS","FULLBATHS","HALFBATHS","FIREPLACES","FINISHEDLIVINGAREA","PREVSALEPRICE","PREVSALEPRICE2")
X[factorCols] <- lapply(X[factorCols], factor)
set.seed(123)
boruta.train<-Boruta(X,Y)
So you see that I have a data set of different features, some of them are string features, so I convert them to factors. The rest is numeric. I test my assumptions:
And once I run the Boruta I get
Error in data.matrix(data.selected) :
(list) object cannot be coerced to type 'double'
I am not sure why. All of my columns are Factors or varoius numeric types. What can be wrong?
After googling a bit I found that some people recommend to do the as.matrix() conversion, but in such case:
> boruta.train<-Boruta(as.matrix(X),as.matrix(Y))
Error: Variable none not found. Ranger will EXIT now.
Error in ranger::ranger(data = x, dependent.variable.name = "shadow.Boruta.decision", :
User interrupt or internal error.
Ok, after playing around with that I managed to identify the problem. Boruta requires Y (target) to be of the list type, not dataframe or anything else.
So just creating Y like this:
Y<-df[,'ClassLabel']
Solves the problem.
I'm trying to compute the inter-rater reliability between 3 raters and I have 7100 rows of data. So for example each row is like this:
5,4,3
> data <- read.csv("/home/xyz/Desktop/tc.csv", header=TRUE)
> library(irr)
Loading required package: lpSolve
> kripp.alpha(data, method="ordinal")
Error in sort.list(y) : 'x' must be atomic for 'sort.list'
Have you called 'sort' on a list?
How do I fix this?
it is probably a bit late, but if you provide a reproducible example, the chances that someone may answer your question are much higher. Otherwise it is really difficult to figure out what went wrong. However I can read from your code that you have fed kripp.alpha with a dataframe (output of read.csv), which is not what the function expects. So you can probably fix the error by converting the dataframe into a matrix with the as.matrix command (The kripp.alpha function expects its input to be a numeric matrix with the observations in the columns and the observer/rater in the rows)
Best regards,
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
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)
Stuck on an error in R.
Error in names(x) <- value :
'names' attribute must be the same length as the vector
What does this error mean?
In the spirit of #Chris W, just try to replicate the exact error you are getting. An example would have helped but maybe you're doing:
x <- c(1,2)
y <- c("a","b","c")
names(x) <- y
Error in names(x) <- y :
'names' attribute [3] must be the same length as the vector [2]
I suspect you're trying to give names to a vector (x) that is shorter than your vector of names (y).
Depending on what you're doing in the loop, the fact that the %in% operator returns a vector might be an issue; consider a simple example:
c1 <- c("one","two","three","more","more")
c2 <- c("seven","five","three")
if(c1%in%c2) {
print("hello")
}
then the following warning is issued:
Warning message:
In if (c1 %in% c2) { :
the condition has length > 1 and only the first element will be used
if something in your if statement is dependent on a specific number of elements, and they don't match, then it is possible to obtain the error you see
I have seen such error and i solved it. You may have missing values in your data set. Number of observations in every column must also be the same.
I want to explain the error with an example below:
> names(lenses)
[1] "X1..1..1..1..1..3"
names(lenses)=c("ID","Age","Sight","Astigmatism","Tear","Class")
Error in names(lenses) = c("ID", "Age", "Sight", "Astigmatism", "Tear", :
'names' attribute [6] must be the same length as the vector [1]
The error happened because of mismatch in a number of attributes. I only have one but trying to add 6 names. In this case, the error happens. See below the correct one:::::>>>>
> names(lenses)=c("ID")
> names(lenses)
[1] "ID"
Now there was no error.
I hope this will help!
I had this, caused by a scaled numeric variable not being returned as numeric, but as a matrix. Restore any transformed variables to as.numeric() and it should work.
The mistake I made that coerced this error was attempting to rename a column in a loop that I was no longer selecting in my SQL. This could also be caused by trying to do the same thing in a column that you were planning to select. Make sure the column that you are trying to change actually exists.
For me, this error was because I had some of my data titles were two names, I merged them in one name and all went well.
I encountered the same error for a silly reason, which I think was this:
Working in R Studio, if you try to assign a new object to an existing name, and you currently have an object with the existing name open with View(), it throws this error.
Close the object 'View' panel, and then it works.