1
votes

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.

2
Maybe you should give example of what datlist looks like.CHP
datlist is a list of three different data frames of varying number of rows and 7 columns. The code above is used to extract similar values from all three data frames.user2040842
try: sapply(dataList, function(x) c(length(unique(x$NDC)), length(unique(x$PAYERID)), length(x$NDC)))adibender

2 Answers

1
votes

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.

1
votes

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")