"함수 mode()"의 두 판 사이의 차이

(새 문서: ==개요== ;함수 mode() ==R== 분류: R <source lang='r'> getmode <- function(v) { uniqv <- unique(v) uniqv[which.max(tabulate(match(v, uniqv)))] } v <- c(2,1,2,3,1,2) prin...)
 
1번째 줄: 1번째 줄:
[[분류: array]]
==개요==
==개요==
;함수 mode()
;함수 mode()
;함수 majority()
==PHP==
[[분류: PHP]]
<source lang='PHP'>
function majority($arr) {
$count_values = array_count_values($arr);
$max = max($count_values);
foreach( $count_values as $key => $count) {
if( $max == $count ) return $key;
}
}
$fruits = array(
'banana',
'orange',
'apple',
'apple',
'orange',
'orange',
'apple',
'orange',
'pineapple',
);
echo majority( $fruits );
# orange
</source>


==R==
==R==
26번째 줄: 53번째 줄:
* [[함수 mean()]]
* [[함수 mean()]]
* [[함수 median()]]
* [[함수 median()]]
* [[함수 array_count_values()]]
* [[최빈값]]
* [[최빈값]]
* [[다수결]]

2017년 12월 18일 (월) 11:49 판

1 개요

함수 mode()
함수 majority()

2 PHP

function majority($arr) {
	$count_values = array_count_values($arr);
	$max = max($count_values);
	foreach( $count_values as $key => $count) {
		if( $max == $count ) return $key;
	}
}
$fruits = array(
	'banana',
	'orange',
	'apple',
	'apple',
	'orange',
	'orange',
	'apple',
	'orange',
	'pineapple',
);
echo majority( $fruits );
# orange

3 R

getmode <- function(v) {
  uniqv <- unique(v)
  uniqv[which.max(tabulate(match(v, uniqv)))]
}
v <- c(2,1,2,3,1,2)
print( getmode( v ) )
# 2
getmode <- function(v) {
  uniqv <- unique(v)
  uniqv[which.max(tabulate(match(v, uniqv)))]
}
charv <- c("o","it","the","it","it")
print( getmode( charv ) )
# "it"

4 같이 보기

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}