0 votes
1 view
in R Programming by (6.5k points)

I have a dataframe, and for each row, in that dataframe I have to do some complicated lookups and append some data to a file.

The dataFrame contains scientific results for selected wells from 96 well plates used in biological research so I want to do something like:

for (well in dataFrame) {

  wellName <- well$name    # string like "H1"

  plateName <- well$plate  # string like "plate67"

  wellID <- getWellID(wellName, plateName)

  cat(paste(wellID, well$value1, well$value2, sep=","), file=outputFile)


In my procedural world, I'd do something like:

for (row in dataFrame) {

    #look up stuff using data from the row

    #write stuff to the file


What is the "R way" to do this?

1 Answer

0 votes
by (25.3k points)

You can use the apply() function to get the desired output as follows:

df <- data.frame(name = c("A","B","C"),

                plate = c("P1","P2","P3"),

                value1 = c(1,2,3),

                value2 = c(100,200,300))

f <- function(x, output) {

  wellName <- x[1]

  plateName <- x[2]

  wellID <- 1

  print(paste(wellID, x[3], x[4], sep=","))

  cat(paste(wellID, x[3], x[4], sep=","), file= output, append = T, fill = T)


apply(d, 1, f, output = 'outputfile')

Welcome to Intellipaat Community. Get your technical queries answered by top developers !