user2040842
user2040842

Reputation: 35

Changing multiple data frame column titles in r

The program that I am running creates three data frames using the following code:

datuniqueNDC <- data.frame(lapply(datlist, function(x) length(unique(x$NDC))))
datuniquePID <- data.frame(lapply(datlist, function(x) length(unique(x$PAYERID)))
datlengthNDC <- data.frame(lapply(datlist, function(x) length(x$NDC)))

They have outputs that look like this:

  X182L X178L X76L
1   182   178   76

  X34L X31L X7L
1   34   31   7

  X10674L X10021L X653L
1   10674   10021   653

What I am trying to do is combine the rows together into one data frame with the desired outcome being:

        X      Y    Z
1     182    178   76
2      34     31    7
3   10674  10021  653

but the rbind command doesn't work due to the names of all the columns being different. I can get it to work by using the colnames command after creating each variable above, but it seems like there should be a more efficient way to accomplish this by using one of the apply commands or something similar. Thanks for the help.

Upvotes: 1

Views: 735

Answers (2)

Jan van der Laan
Jan van der Laan

Reputation: 8105

Since the functions you use in you lapply calls are scalars, it would be easier if you use sapply. sapply returns vectors which you can rbind

datuniqueNDC <- sapply(datlist, function(x) length(unique(x$NDC)))
datuniquePID <- sapply(datlist, function(x) length(unique(x$PAYERID))
datlengthNDC <- sapply(datlist, function(x) length(x$NDC))
dat <- as.data.frame(rbind(datuniqueNDC,datuniquePID,datlengthNDC))
names(dat) <- c("x", "y", "z")

Another solution is to calculate all three of your statistics in one function:

dat <- as.data.frame(sapply(datlist, function(x) {
    c(length(unique(x$NDC)), length(unique(x$PAYERID), length(x$NDC))
}))
names(dat) <- c("x", "y", "z")

Upvotes: 1

user1317221_G
user1317221_G

Reputation: 15461

one way, since evreything seems to be a numeric, would be this:

mylist <- list(dat1,dat2,dat3) 
# assuming your three data.frames are dat1:dat3 respectively

 do.call("rbind",lapply(mylist, as.matrix))
#     X182L X178L X76L
#[1,]   182   178   76
#[2,]    34    31    7
#[3,] 10674 10021  653

basically this works because your data are matrices not dataframes, then you only need to change names once at the end.

Upvotes: 1

Related Questions