0

I have a list of dataframes (dflist) and data I want to compare each data frame against (actual)

A sample of what the data looks like:

    dflist[['2Source'}}
    Chem 1 Chem 2 
    .01     .02
    .02     .03
    .01     .03

    actual
  Chem 1 Chem 2 
    .01     .02
    .03     .02
    .01     .04

I want to plot each column against the identical column in the other table so, dflist[['2Source']][Chem 1] vs actual[Chem 1] and for chem 2. My actual data has 18 chemicals so I'm trying to write something with lapply to look all of them.

par(mfrow=c(3,4))

Chemnames <- names(dflist[['2Source']])

baseplot <- function(x,y, name) {
  plot(x, y, main = name)
  abline(lm(y~x))}

lapply(seq_along(dflist), function(i)
  mapply(function(x,y, name) {
    plot(x, y, main = names(Chemnames(name)))
    abline(lm(y~x))}, x = actual, y = dflist[[i]]))

This is my current code which seems to work minus the labels and I can't figure it out.

1 Answer 1

1

Maybe this is what you are looking for:

dflist <- list()

dflist$`2Source` <- data.frame(
  `Chem 1` = c(.01, .02, .01), 
  `Chem 2` = c(.02, .03, .02))

actual <- data.frame(
  `Chem 1` = c(.01, .03, .01), 
  `Chem 2` = c(.02, .02, .04))

par(mfrow=c(3,4))

baseplot <- function(x, y, name) {
  plot(x, y, main = name)
  abline(lm(y ~ x))
}

# Just to check that we get the correct plots
baseplot(actual$Chem.1, dflist$`2Source`$Chem.1, "Chem.1")
baseplot(actual$Chem.2, dflist$`2Source`$Chem.2, "Chem.2")
# Now with lapply
lapply(dflist, function(df) mapply(baseplot, x = actual, y = df, name = names(df)))
#> $`2Source`
#> $`2Source`$Chem.1
#> NULL
#> 
#> $`2Source`$Chem.2
#> NULL

Sign up to request clarification or add additional context in comments.

3 Comments

I had a similar but equivalent idea - chemnames <- names(actual); Map(function(dl, a) Map(baseplot, dl[chemnames], a, name=chemnames), dflist, list(actual)) - the only possible catch is that the order of the names is assumed to be the same in your answer I think.
@thelatemail Yep. You are right. My approach only works if the columns are in the same order. If not we first have to reorder as you do in your Map approach. Feel free to post it as an alternative and more robust approach.
Thank for both of your help! I'm not super familiar with the Map function, it did the trick!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.