Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How to write a function that takes as arguments: 1) a data frame, 2) a column nu

ID: 3041316 • Letter: H

Question

How to write a function that takes as arguments: 1) a data frame, 2) a column number

for that data frame, and outputs: 1) the maximum value of that column ; 2) the row

index (or patient) corresponding to that maximum value; 3) the minimum value of

that column; 4) its corresponding row index.

The answer should following the way like this:

> max . calc for (j in 3:8) print (max.calc(dat,j)) max.val max.ind min.val min.ind 114 121 119 118 max.val max.ind min.val min.ind 7 max.val max.ind min.val min.ind 7 27 100 max.val max.ind min.val min.ind 8 27 102 max.val max.ind min.val min.ind 10 115 24 97 max.val max.ind min.val min.ind 24 63 18 38

Explanation / Answer

R Codes

max.cal=function(data,cn)
{
OP=data.frame(max.val=max(data[,cn]),max.ind=which.max(data[,cn]),min.value=min(data[,cn]),min.ind=which.min(data[,cn]))
return(OP)
}

Example

data=read.table(file.choose(),header=T)

for(i in 2:6)
print(max.cal(data,i))

max.val max.ind min.value min.ind
1 18.5 32 0.58 1
max.val max.ind min.value min.ind
1 1.15 5 1 31
max.val max.ind min.value min.ind
1 1.66 26 0.25 1
max.val max.ind min.value min.ind
1 1 5 0.7 24
max.val max.ind min.value min.ind
1 0.068 32 0.013 1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote