0

I am new to R. I have 3 data frames WT1, WT2,WT3 with 5000 rows and 3 columns c1,c2,c3.

I have already written code and created a data frame called "Result" with same 5000 rows and two columns(M1,M2) containing the maximum and minimum values obtained by comparing each corresponding row of all the five data frames using for loop.

Though i have max and min values in M1,M2 of "Result" for each row of WT1, WT2,WT3, i dont know in which data frame this Max value occurs and in which dat frame the Min value occurs. So i should find in which data frame the M1(max) value occurs and should display the correcponding data frame's c1 element in a separate column M3 in "Result". Similarly the code should find from which data frame the M2(min) value occurs and display the corresponding data frame's c1 elemnt in a separate column called M4 in "Result". The code should do this for all 5000 rows in "Result" to display M3 and M4.

Note: The c1 element will be in alphanumeric format(04WEA804500), So i guess i should also convert it to numeric before displaying it in M3 and M4..Sorry for not pasting the data frames here as it is large.....Plesae help me..

Thanks in advance..

  • Take a look at `which` and its pals `which.max`, `which.min` . you may find this a lot easier if you use `cbind` to merge your `WT*` into a single matrix (not dataframe). – Carl Witthoft Nov 11 '13 at 14:10
  • Hi and welcome to SO! You are much more likely to receive a helpful answer if you provide a [minimal, reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) together with the code you have tried. Thanks! – Henrik Nov 11 '13 at 14:33
  • Which column(s) is used to find the `min` and `max` between rows? I suppose not `c1`. `c2` & `c3`? Or, which one of them? – alexis_laz Nov 11 '13 at 17:16
  • Hi Henrik...Actually i pasted a sample of my data frames from Excel in the above description. But when i submit the question, the pasted data frame are changing from table format to line format displaying all the values in paragraphs same like text.Can you please tell me how should i display it in the same table format? – user2979160 Nov 12 '13 at 10:14
  • Hi alexis_laz..I am comparing the c2 column value of WT1, WT2,WT3 to get the maximum and minimum value. – user2979160 Nov 12 '13 at 10:17

1 Answers1

0

There has to be a more elegant way but I'm stuck with this; hope it helps:

#some random data at first
set.seed(1)
WT1 <- data.frame(c1 = replicate(3, paste(sample(LETTERS, 4), 
      sample(0:9, 4), sep = "", collapse = "")), c2 = runif(3), stringsAsFactors = F)
set.seed(2)
WT2 <- data.frame(c1 = replicate(3, paste(sample(LETTERS, 4), 
      sample(0:9, 4), sep = "", collapse = "")), c2 = runif(3), stringsAsFactors = F)
set.seed(3)
WT3 <- data.frame(c1 = replicate(3, paste(sample(LETTERS, 4), 
      sample(0:9, 4), sep = "", collapse = "")), c2 = runif(3), stringsAsFactors = F)
#> WT1                    #> WT2                   #> WT3
#        c1         c2    #        c1        c2    #        c1        c2
#1 G2J8N7U4 0.26722067    #1 E9R8N1D5 0.3472722    #1 E6U5J0H2 0.2368850
#2 Q6B3E9X8 0.38611409    #2 M7N1Y3F5 0.4887732    #2 P5Z9M6L8 0.7911474
#3 S9Y1J5R0 0.01339033    #3 Z6F3K9B1 0.1492469    #3 C2R0V1G8 0.5997316

#separate columns `c1` from `c2` from all dataframes, in a manipulative form
c1s <- mapply(function(...) c(...), WT1$c1, WT2$c1, WT3$c1, SIMPLIFY = F)
names(c1s) <- NULL           
c2s <- mapply(function(...) c(...), WT1$c2, WT2$c2, WT3$c2, SIMPLIFY = F)

#a function to calculate everything wanted...
fun <- function(c1, c2)
{
 M1 <- max(c2)
 M2 <- min(c2)
 M3 <- c1[which.max(c2)]
 M4 <- c1[which.min(c2)]

 data.frame(M1, M2, M3, M4, stringsAsFactors = F)
}

#...for each row
Results <- t(mapply(fun, c1s, c2s))
#> Results
#     M1        M2         M3         M4        
#[1,] 0.3472722 0.236885   "E9R8N1D5" "E6U5J0H2"
#[2,] 0.7911474 0.3861141  "P5Z9M6L8" "Q6B3E9X8"
#[3,] 0.5997316 0.01339033 "C2R0V1G8" "S9Y1J5R0"
alexis_laz
  • 12,884
  • 4
  • 27
  • 37