BAMF4bacon
BAMF4bacon

Reputation: 593

In R, how do I use apply() with data frames?

apply() allows you to select whether rows or columns with MARGIN=1 or MARGIN=2, respectively.

But apply only works on matrices.

For example, I have three rows of header information and 3 columns of descriptive information. I need to combine these into row names and row names and column names, respectively. So I can't easily use read.table() and skip the first 3 rows, and then delete the first 3 columns to get my matrix right away.

This doesn't work on data frames, just matrices

rownames(df)<-apply(df[,1:3], MARGIN=1,FUN=function(x){paste(x,sep=".")})

Upvotes: 2

Views: 14166

Answers (2)

sync11
sync11

Reputation: 1280

You can exclude the non-numeric columns/rows and deploy apply function onto the numeric rows.

Consider an example: If a data frame has 4 columns out of which the first one belongs to the character class, then use below code:

apply(Data.df[,2:4],2,func_name)

Upvotes: 1

BAMF4bacon
BAMF4bacon

Reputation: 593

First, please note this is not a problem for columns. lapply will do columns without trouble

colnames(df) <- lapply(df[1:3,], function(x) {paste(x,sep=".")})

A great answer by rsoren is found here). Modified for this problem:

for (row in 1:nrow(df)) { 
    rownames(df)[row] <- paste(df[row,1:3],sep=".") 
}

You can't do rows with lapply. Here is an alternate, more intuitive approach. To operate on rows, we make the transpose first; now the rows are columns and lapply can operate on the columns.

tdf<-as.data.frame(t(df))
rownames(df) <- lapply(tdf[1:3,], function(x) {paste(x,sep=".")})

The only downside to this is that R makes a copy of the table during the transpose, so it doubles memory usage. However, rsoren's method does not.

Now I can delete the extra rows and columns and make the matrix I want with proper row and column names.

myMatrix <- as.matrix(df[-(1:3),-(1:3)])

Upvotes: 0

Related Questions