Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8163043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:55:49+00:00 2026-06-06T18:55:49+00:00

how do I access the actual used factor combination in a by() call? I

  • 0

how do I access the actual used factor combination in a by() call?
I want to plot results from a dbscan (fpc-package) and add the corresponding factor combination to the plot title. The output is of class “by”.
Using attributes(res.lst) respectively attr(res.lst,"dimnames") gives me only a global list . I cant map that to the individual used subset of data. So how does the by function “know” which combination is actual used for which subset. Where is this information stored?

I tried to make an easy example (skipping dbscan) to make it clear what the desired output should look like. Any help? I want to stick to base R if it’s possible.

dat <- data.frame(x=rnorm(1000), y=rnorm(1000), fac1=rep(c("L1", "L2"), 500), fac2= as.factor(c(rep(1, 5), rep(2, 5) )) ) # dummy data
# dummy plot function
fun.plot <- function(data, factorcomb=NA) { 
  if (is.na(factorcomb)==T) {
    ttl <- "Standard"
  } else { 
    ttl <- paste("factor combination: ", paste(factorcomb, collapse="+"))
  }
  dev.new()
  plot(data$x, data$y, main=ttl) 
}
res.lst <- by(data=dat, INDICES=dat[ ,colnames(dat[ , c(3,4)]) ], function(splt) fun.plot(splt) )

# desired result
for (f1 in levels(dat$fac1) ) {
  for (f2 in levels(dat$fac2) ) {
    fun.plot(dat[ which( dat$fac1==f1 & dat$fac2==f2), 1:2 ], factorcomb=c(f1,f2) )
   }
}

EDIT: This is the call from my original function.

 # by(data=adat, INDICES=adat[ ,colnames(adat[ ,c(36,41) ]) ], FUN=fun.dbscan, factornames=colnames(adat[ ,c(36,41) ]))

And the output on the coonsole looks like that:

PeakTemp: 250 # Factor 1 (column number 36) + Level
BGANr: BGA196_01 #Factor 2 (column number 41) + Level
dbscan Pts=1551 MinPts=12 eps=0.015
          0  1
seed      0 17
border 1534  0
total  1534 17
etc...

I want to pass actual used factor combination to the fun.dbscan function. In that case it would be PeakTemp with level 250 and BGANr BGA196_01

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T18:55:51+00:00Added an answer on June 6, 2026 at 6:55 pm

    This is similar to what @joran did, but a bit different.

    fun.plot <- function(data) {
      #change 3:4 if you have more variables
      factorcomb <- as.character(sapply(data[, 3:4], FUN = unique)) 
      ttl <- paste("factor combination: ", paste(factorcomb, collapse="+"))
      dev.new()
      plot(data$x, data$y, main=ttl) 
    }
    res.lst <- by(data = dat, INDICES = dat[ , 3:4], FUN = fun.plot)
    

    EDIT

    If you want to pass custom level names (e.g. nms), you can try using mapply. You can fiddle around to format ttl the way you want it, like replacing dot with a space.

    dat <- data.frame(x=rnorm(1000), y=rnorm(1000), fac1=rep(c("L1", "L2"), 500), fac2= as.factor(c(rep(1, 5), rep(2, 5) )) ) # dummy data
    dat.split <- split(dat, dat[, 3:4])
    nms <-names(dat.split)
    
    fun.plot <- function(data, custom.names) {
      ttl <- paste("factor combination: ", custom.names)
      dev.new()
      plot(data$x, data$y, main=ttl) 
    }
    mapply(fun.plot, dat.split, nms)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

PHP - Access MSSQL datetime column from the returned array Array ( [0] =>
Which access modifiers which when used with the method makes it available to all
i am new to objective c programming I want to call a function within
Access 2007 is telling me that my new expression is to complex. It used
Previously i have used restful wcf webservices to get the data from server.But now
I am looking for a way to import a datatable from Access into an
I have a project in Access 97 that I have to add the ability
I am developing an add in for C# that would only be used during
I'm writing a data access layer where I want to return just the data
Does anyone have access to a list of the default colours used in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.