Rbind Error: "Names Do Not Match Previous Names"

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: Error with the 'rbind' function while trying to add data frames converted from csv files

The problem is that your initial data frame does not have the same column names, as those read from your files. You could construct it with the correct names but it is much easier to let it be NULL and initialize like this:

master.data.frame <- NULL

Of course r2evans is absolutely right that repeated rbinding is not at all efficient. But it may be good enough – you decide for your application.

And some unsolicited remarks:

I would avoid to use setwd and instead construct the correct path to the data using file.path and use that in read.csv.

I would also try to avoid addressing the columns of a data.frame by numeric index and use the column names, instead.

Finally – read.csv already returns a data.frame – no need to cast it.

Here's a slightly cleand up version:

pollutantmean <- function(directory, pollutant, id) {
master.data.frame <- NULL
allfiles <- list.files(directory)
for (i in id) {
thisfile <- read.csv(file.path(directory, allfiles[i]))
master.data.frame <- rbind(master.data.frame,thisfile)
}

means <- colMeans(master.data.frame[pollutant],na.rm = TRUE)
print(means)
}


Related Topics



Leave a reply



Submit