duplicate rownames and colnames

I have read my file into R using read.csv, read.delim, read.table, read.excel functions but none allow me to have duplicated row names and colnames. I tried various parameters in functions but none worked for me.

map <- read.delim(metafile, sep = ",")

rownames(map) <- map$Sites

dput(map)
structure(list(Sites = c("Site 1", "Site 1", "Site 1", "Site 1",
"Site 2", "Site 2", "Site 2", "Site 2", "Site 3", "Site 3", "Site 3",
"Site 3"), Months = c("Aug", "Sep", "Oct", "Nov", "Aug", "Sep",
"Oct", "Nov", "Aug", "Sep", "Oct", "Nov")), class = "data.frame", row.names = c(NA,
-12L))

I get the following error:

Error in .rowNamesDF<-(x, value = value) :
duplicate 'row.names' are not allowed

The output I want is as follows

I want rownames exactly same as the column "Sites".
How can I get the desired output?

Regards
Hira

They are not allowed. My understanding is that row.names is something like a unique key in SQL.

Why do you want duplicate row.names? It is very likely that there are alternative methods to do what you want.

I am doing an analysis using phyloseq package in R. For that purpose I have to create a phyloseq object.
For this I am using function sample_data().

#transform dataframe into phyloseq class object.
map <- sample_data(map)

This requires the rownames to be correctly named just as my column "Sites".

rownames(map) <- map$Sites

Because in the later steps of analysis, I get errors.

Can you please suggest any method to do this?

You will need to provide more code showing what you are doing later on because

map <- sample_data(map)

works fine and assigns unique row.names.

Where are your errors occurring and what are the error messages?

1 Like

My code goes as follows:

map <- structure(list(Sites = c("Site 1", "Site 1", "Site 1", "Site 1",
                         "Site 2", "Site 2", "Site 2", "Site 2", "Site 3", "Site 3", "Site 3",
                         "Site 3"), Months = c("Aug", "Sep", "Oct", "Nov", "Aug", "Sep",
                           "Oct", "Nov", "Aug", "Sep", "Oct", "Nov")), 
                 class = "data.frame", row.names = c(NA, -12L))

# transform dataframe into phyloseq class object.
map <- sample_data(map)

otu.table <- read.csv(file = "mouse.csv", header = TRUE, sep = ",")

dput(otu.table)
structure(list(`Site 1` = c(1, 5, 1, 0, 0, 0, 0, 0, 3, 0, 0, 
0, 0, 0, 0, 0, 0, 1, 2, 4, 2, 3, 2, 0, 0, 0, 0, 1, 1, 0,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c(5, 1, 
0, 0, 0, 0, 0, 3, 5, 0, 0, 2, 0, 0, 0, 0, 0, 0, 3, 7, 1, 0, 0, 
0, 0, 2, 0, 0, 3, 1, 0, 5, 0, 0, 0, 0, 0, 0), `Site 3` = c(14, 
0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 3, 0, 0, 
0, 1, 0, 0, 0, 0, 3, 1, 0, 4, 2, 0, 1, 1, 0, 2), `Site 3` = c(22, 
0, 0, 0, 0, 0, 0, 4, 2, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0)), row.names = c("otu001", 
"otu002", "otu003", "otu004", "otu005", "otu006", "otu007", "otu008", 
"otu009", "otu010", "otu011", "otu012", "otu013", "otu014", "otu015", 
"otu016", "otu017", "otu018", "otu019", "otu020", "otu021", "otu022", 
"otu023", "otu024", "otu025", "otu026", "otu027", "otu028", "otu029", 
"otu030", "otu031", "otu032", "otu033", "otu034", "otu035", "otu036", 
"otu037", ""), class = "data.frame")


rownames(otu.table)<- otu.table$otus

otu.table <- otu.table[, colnames(otu.table) != "otus"]

colnames(otu.table) <- c("Site 1", "Site 1", "Site 1", "Site 1",
                         "Site 2", "Site 2", "Site 2", "Site 2", "Site 3", "Site 3", "Site 3",
                         "Site 3")

otu.table[is.na(otu.table)] <- 0

otu_mat <- as.matrix(otu.table)

# transform dataframe into phyloseq class object.
otu_mat <- otu_table(otu_mat, taxa_are_rows = T)

Phyloseq_object <- merge_phyloseq(otu_mat, map)

My mapfile looks like this

The row names are not correct here.
When I use

merge_phyloseq(otu_mat, map)

I get this error:
Error in validObject(.Object) : invalid class “phyloseq” object:
Component sample names do not match.
Try sample_names()

And I cannot proceed further.
Duplicating rownames are possible. You can check the code here:

I tried it on mouse.dpw.metadata of this link(https://zenodo.org/record/800651#.ZBbaCuxBxJU).
It works but not on my metadata file. I cannot figure it out.
I hope I was able to explain my query clearly.
regards
Hira

I am not a {phyloseq } user so this may be completely wrong but this code seems to work.

library( phyloseq )
map <- sample_data(map)
dat1  <- sample_data(otu.table)
Phyloseq_object <- merge_phyloseq(dat1, map)

Most of your code appears unneeded.

We have a 38 X 12 data.frame otu.table with row.names ranging from "otu001" "otu002" to "otu037" "" where the 38th row.name is blank and with colnames: "Site 1" "Site 1" "Site 1" "Site 1" "Site 2" "Site 2" "Site 2" "Site 2" "Site 3" "Site 3" "Site 3" "Site 3"

The statement below has reset the row.names to 1:38, that is, no row.names since otu.table$otus does not exist and has a valuu of NULL

rownames(otu.table) <- otu.table$otus

You then do

otu.table <- otu.table[, colnames(otu.table) != "otus"]

seemingly, to drop otu.table$otus but it does not exist.

Then you have renamed the *otu.table colnames

otu.table <- otu.table[, colnames(otu.table) != "otus"]

to "Site 1" "Site 1.1" "Site 1.2" "Site 1.3" "Site 2" "Site 2.1" "Site 2.2" "Site 2.3" "Site 3" "Site 3.1" "Site 3.2" "Site 3.3" and then immediately rename them to their original names.

colnames(otu.table) <- c("Site 1", "Site 1", "Site 1", "Site 1",
                         "Site 2", "Site 2", "Site 2", "Site 2", "Site 3", "Site 3", "Site 3",
                         "Site 3")

Why?

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.