Doa
Doa

Reputation: 23

How do I combine the values in two different columns with same names?

I have a matrix that has multiple columns with different names but some of them are the same and I want to group them together, then take the mean of those columns.

Basically,

Lung Lung Lung Heart Heart Heart Kidney Kidney Kidney Liver Liver Liver
 3     7   9     1     2     1     8      9      12     15    17    19

I want the matrix above to look like this

Lung Heart Kidney Liver
 3     1      8     15
 7     2      9     17
 9     1      12    19

Then I am planning to get the mean of each column (Lung, Heart etc.)

So, it would also be fine if you suggest a code that calculates the means of the columns that have the same names without even combining them.

Upvotes: 1

Views: 188

Answers (2)

lmo
lmo

Reputation: 38520

Here is a method with matrix to organize the values into columns and setNames to add the variable names. Note that this requires that the variables with the same name are of equal size and are adjacent.

setNames(data.frame(matrix(unlist(dat), 3)), unique(names(dat)))
  Lung Heart Kidney Liver
1    3     1      8    15
2    7     2      9    17
3    9     1     12    19

Also, this solution works with a data.frame rather than a matrix, because this is the structure presented in the question, but the same code will work with a matrix without any alterations.

data

dat <-
structure(list(Lung = 3L, Lung = 7L, Lung = 9L, Heart = 1L, Heart = 2L, 
    Heart = 1L, Kidney = 8L, Kidney = 9L, Kidney = 12L, Liver = 15L, 
    Liver = 17L, Liver = 19L), .Names = c("Lung", "Lung", "Lung", 
"Heart", "Heart", "Heart", "Kidney", "Kidney", "Kidney", "Liver", 
"Liver", "Liver"), class = "data.frame", row.names = c(NA, -1L
))

Upvotes: 1

akrun
akrun

Reputation: 887901

We can use split

data.frame(lapply(split.default(df1, names(df1)), unlist, use.names = FALSE))
#   Heart Kidney Liver Lung
#1     1      8    15    3
#2     2      9    17    7
#3     1     12    19    9

Upvotes: 2

Related Questions