R language - Unrecognized Error relating rbind - r

Has anyone seen this error before? -
Error in colnames<-(tmp, value = 1:59) :
'names' attribute [59] must be the same length as the vector [0]
If yes, how can I fix with it?

This error would occur, if you try to assign column names to an empty data.frame:
d <- data.frame()
colnames(d) <- 1:59

Related

R error: ' Error: `f` must be a factor (or character vector).'

I'm getting this error :
Error: f must be a factor (or character vector).
Here is my code
ge19 <- read.csv("ge2019.csv")
aps19 <- read.csv("aps19.csv")
ge19aps19 <- merge(ge19, aps19,by="ons_id")
ge19aps19$london <- ge19aps19$region_name
table(ge19aps19$london)
library (dplyr)
library(forcats)
ge19aps19$london <- fct_drop(ge19aps19$london)
table(ge19aps19$london)
ge19aps19$london <- relevel(ge19aps19$london, ref= "London")
table(ge19aps19$london)
ge19aps19$lab.per <- ge19aps19$lab/ge19aps19$valid_votes
ge19aps19$lab.per <- fct_drop(ge19aps19$lab.per)
Can anyone tell me what's wrong? first time user of this site so please let me know if there's more information needed / I've formatted my question wrong
The error message mean that the param you passed to fct_drop function is neighther factor or character.
And from the your code I saw that ge19aps19$lab.per is a numeric column calculated by this formula
ge19aps19$lab.per <- ge19aps19$lab/ge19aps19$valid_votes
Why you run fct_drop on that column? It is a numeric column so fct_drop threw an error message there!

R Studio: Creating a empty list results in an Error: 'names' attribute [1] must be the same length as the vector [0]

I am quite new to R and tried to create an empty list (names Input_IMPLEMENTATION) using:
Input_IMPLEMENTATION<-vector(mode="list",length=7)
However, this resulted in the following error:
Error in names(x) <- paste("V", seq_along(x), sep = "") :
'names' attribute [1] must be the same length as the vector [0]
If I use a different name for the list, everything works fine. for example:
Input_IMPLEMENTATION_a<-vector(mode="list",length=7)
Therefore, I assumed the error occurs as Input_IMPLEMENTATION is allready assigned some value. So I tried rm(Input_IMPLEMENTATION) before executing the code, but this did not change anything.
Does anyone have a hint for me, why I get this error?

R: error in dimnames when using colnames with matrix

Imagine the case to preallocate a 2x2 matrix with NAs. Now I would like to rename the first column to "Test" with
name_matrix<-matrix(NA,2,2)
colnames(name_matrix)[1] <- "Test"
But now I am getting the error message:
Error in dimnames(x) <- dn : length of 'dimnames' [2] not equal to array extent
Whereas if I convert the matrix to a data.frame first I am not ending up with this error message.
name_data_frame<-as.data.frame(name_matrix)
colnames(name_data_frame)[1] <- "Test"
My question is does anybody have an idea how to find out more about this unexpected behavior and maybe as well how to fix it?
The problem is that after the first line
name_matrix<-matrix(NA,2,2)
the value of colnames(name_matrix) is NULL for which it does not make sense to access its first entry colnames(name_matrix)[1].
Instead you have to assign the entire variable a vector of length 2.
For instance you could do:
colnames(name_matrix)<- c("Test", NA)

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']])) )

'names' attribute must be the same length as the vector

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.

Resources