Wednesday, 15 September 2010

r - Creating Function Utilizing Conditional Counting -



r - Creating Function Utilizing Conditional Counting -

i want develop function automate process of grade analyzing.

in 1 column, there number of tutoring hours pupil had , in next column or grade.

for 3 groups, have count number of (a+s , a-s), bs (b+s , a-s), cs, ds, fs, ws , calculate gpa of each. groups had 6 or more hours, had no hours, , total number of had tutoring.

is possible create simple function in r, such grades(hours,mark), similar summary() function, print 3 categories next info each:

number of students type(as include a-s, as, , a+s,etc.) of a, b, c, d, f, w calculate gpa: (4*a+3*b+2*c+1*d+0*f+0*w)/(a + b+ c + d+ f - w)

is possible in r? start?

thanks.

> hours=c(0,5,6,10,0);grades=c("f","a-","a","a+","c-") > grphrs <- hours >= 6 > table( substr(grades, 1,1), grphrs) grphrs false true 1 2 c 1 0 f 1 0

hard know sort of gpa calculation needed since clear these different students. if these same pupil then:

grdabcdfw <- substr(grades,1,1) sum( 4* (grdabcdfw=="a")+ 3*(grdabcdfw=="b")+ 2*(grdabcdfw=="c")+ 1*(grdabcdfw=="d")+ 0*(grdabcdfw=="f")+ 0*(grdabcdfw=="w") )/ length(grades[ !is.na(grades)] ) #[1] 2.8

to comment. perhaps:

hours.time=function(hours, grades, cutpt){ grphrs <- hours >= cutpt tbl <- table( substr(grades,1,1), grphrs) colnames(tbl) <- c( paste("fewer ", cutpt), paste( cutpt, "or more") ) tbl } hours.time(hours, grades,6) #------- grphrs fewer 6 6 or more 1 2 c 1 0 f 1 0

r conditional

No comments:

Post a Comment