rbind error: names do not match previous names
The names (column names) of the first dataframe do not match the names of the second one. Just as the error message says.
> identical(names(xd.small[[1]]), names(xd.small[[2]]) )
[1] FALSE
If you do not care about the names of the 3rd or 4th columns of the second df, you can coerce them to be the same:
> names(xd.small[[1]]) <- names(xd.small[[2]])
> identical(names(xd.small[[1]]), names(xd.small[[2]]) )
[1] TRUE
Then things should proceed happily.
Error in match.names(clabs, names(xi)) : names do not match previous names
Its actually the most primal thing to do in R:
data:
df1 <- data.frame(boys_age = c(18,15,16,17,19), girls_age = c(16,14,18,17,15))
code:
library(data.table)
melt(setDT(df1), variable.name = "group", value.name = "age", measure.vars = c("boys_age", "girls_age"))[,2:1][,group:=sub("_.*$","",group)][]
result:
# age group
# 1: 18 boys
# 2: 15 boys
# 3: 16 boys
# 4: 17 boys
# 5: 19 boys
# 6: 16 girls
# 7: 14 girls
# 8: 18 girls
# 9: 17 girls
#10: 15 girls
You seem to be keen on using ?rbind
: (not practical though)
rbind(
cbind.data.frame(age = df1$boys_age, group = "boys"),
cbind.data.frame(age = df1$girls_age, group = "girls")
)
# age group
#1 18 boys
#2 15 boys
#3 16 boys
#4 17 boys
#5 19 boys
#6 16 girls
#7 14 girls
#8 18 girls
#9 17 girls
#10 15 girls
- In the
?cbind
section I'm making use of the recycling functionality R provides. Read about it. - Why am I using
cbind.data.frame
, otherwise cbind would create a matrix and therefore the age numerics would be converted to characters.
rbind produces Error in match.names(clabs, names(xi))
See names(df1)==names(df2)
. You'll find that the name of variable #47 differs between df1
and df2
, which causes the error.
To fix this issue, the variable names must be the same. To resolve this issue, you can do:
names(df1)[47]=c("actions.mention") #change variable name
names(df1)==names(df2) #ensure names are the same
rbind(df1,df2) #rbind two data frames.
R cannot melt data.frame
It works for me. I did the following.
library(reshape2)
tableMS <- read.table(text=' X Y Z T
1 375 855 455.7259 3777.856
2 395 969 347.8306 2506.7
3 449 811 309.9512 519.8513
4 451 774 278.291 717.8705
5 453 774 278.291 717.8705
6 455 774 278.291 717.8705
7 521 697 376.734 693.8541
8 529 855 455.7259 3777.856
9 531 855 455.7259 3777.856
10 609 774 278.291 717.8705',header=TRUE)
EDIT This still work even if you coerce Z
and T
to a list.
tableMS$Z <- as.list(tableMS$Z)
tableMS$T <- as.list(tableMS$T)
MeltTable <- melt(tableMS,id=c("X","Y"))
# MeltTable
# X Y variable value
# 1 375 855 Z 455.7259
# 2 395 969 Z 347.8306
# 3 449 811 Z 309.9512
# 4 451 774 Z 278.2910
# 5 453 774 Z 278.2910
# 6 455 774 Z 278.2910
# 7 521 697 Z 376.7340
# 8 529 855 Z 455.7259
# 9 531 855 Z 455.7259
# 10 609 774 Z 278.2910
# 11 375 855 T 3777.8560
# 12 395 969 T 2506.7000
# 13 449 811 T 519.8513
# 14 451 774 T 717.8705
# 15 453 774 T 717.8705
# 16 455 774 T 717.8705
# 17 521 697 T 693.8541
# 18 529 855 T 3777.8560
# 19 531 855 T 3777.8560
# 20 609 774 T 717.8705
edit don't work with reshape2 version 1.4.2
A workaround is to use data.table
package. BTW this solution is faster.
library(data.table)
tableMS$Z <- as.vector(as.list(tableMS$Z))
tableMS$T <- as.vector(as.list(tableMS$T))
setDT(tableMS)
melt(tableMS,id=c("X","Y"))
Maxent: Error in match.names(clabs, names(xi)) : names do not match previous names
That is a bug that occurs when you make a maxent model with only one predictor. You can see it with the data from the example in ?maxent
library(dismo)
# example data
predictors <- stack(list.files(path=paste(system.file(package="dismo"), '/ex', sep=''), pattern='grd', full.names=TRUE ))
occ <- read.table(paste(system.file(package="dismo"), '/ex/bradypus.csv', sep=''), header=TRUE, sep=',')[,-1]
bg <- randomPoints(predictors, 1000)
# this works
me <- maxent(x=predictors[[1:2]], p =occ)
# fails
me <- maxent(x=predictors[[1]], p =occ)
#Error in match.names(clabs, names(xi)) :
# names do not match previous names
This is because with a single layer, the matrix is dropped (the cause of many R bugs...), illustrated here:
extract(predictors[[1:2]], occtrain[1:2,])
# bio1 bio12
#[1,] 263 1639
#[2,] 263 1639
extract(predictors[[1]], occtrain[1:2,])
#[1] 263 263
I will fix that. But here is are two work-arounds.
= Either make a single layer RasterStack (as suggested by you); the simplest approach:
prd <- stack(predictors[[1]])
me <- maxent(x=prd, p =occ)
= Or make a data.frame with extracted raster values for presence and background points:
abs <- cbind(pa=0, bio1=extract(predictors[[1]], bg))
prs <- cbind(pa=1, bio1=extract(predictors[[1]], occ))
and use these data to build the maxent model
x <- data.frame(rbind(prs, abs))
m <- maxent(x[,2,drop=F], p=x[,1,drop=F] )
p <- predict(predictors, m)
plot(p)
Related Topics
How to Create a Variable of Rownames
Calculate Summary Statistics (E.G. Mean) on All Numeric Columns Using Data.Table
Creating Zip File from Folders
R - Reading Lines from a .Txt-File After a Specific Line
Gap in Polar Time Plot - How to Connect Start and End Points in Geom_Line or Remove Blank Space
Escaping "@" in Roxygen2 Style Documentation
How to Use the Spread Function Properly in Tidyr
Consistent Factor Levels for Same Value Over Different Datasets
Marginal Effects of Mlogit in R
Efficient Multiplication of Columns in a Data Frame
How to Extract Multiples of a Number from a Vector
Select Last Row by Group for All Columns Data.Table
Replace Rbind in For-Loop with Lapply? (2Nd Circle of Hell)
Assign Colors to a Range of Values
Extract Columns from Data Table by Numeric Indices Stored in a Vector