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

 
(사용자 3명의 중간 판 6개는 보이지 않습니다)
3번째 줄: 3번째 줄:
;함수 mode()
;함수 mode()
;함수 getmode()
;함수 getmode()
;함수 majority()


==PHP==
==PHP==
[[분류: PHP]]
[[분류: PHP]]
<source lang='PHP'>
{{참고|PHP 최빈값 구하기 mode()}}
function majority($arr) {
<syntaxhighlight lang='PHP' run>
function mode($arr) {
$count_values = array_count_values($arr);
$count_values = array_count_values($arr);
$max = max($count_values);
$max = max($count_values);
foreach( $count_values as $key => $count) {
foreach( $count_values as $key => $count) if( $max == $count ) return $key;
if( $max == $count ) return $key;
}
}
}
$fruits = array(
$fruits = ['orange','banana','apple','orange','orange','apple'];
'banana',
echo mode( $fruits ); # orange
'orange',
</syntaxhighlight>
'apple',
'apple',
'orange',
'orange',
'apple',
'orange',
'pineapple',
);
echo majority( $fruits );
# orange
</source>


==R==
==R==
[[분류: R]]
[[분류: R]]
<source lang='r'>
{{참고|R getmode()}}
<syntaxhighlight lang='r'>
getmode <- function(v) {
getmode <- function(v) {
   uniqv <- unique(v)
   uniqv <- unique(v)
40번째 줄: 28번째 줄:
print( getmode( v ) )
print( getmode( v ) )
# 2
# 2
</source>
</syntaxhighlight>
<source lang='r'>
<syntaxhighlight lang='r'>
getmode <- function(v) {
getmode <- function(v) {
   uniqv <- unique(v)
   uniqv <- unique(v)
49번째 줄: 37번째 줄:
print( getmode( charv ) )
print( getmode( charv ) )
# "it"
# "it"
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==

2021년 12월 5일 (일) 19:45 기준 최신판

1 개요[ | ]

함수 mode()
함수 getmode()

2 PHP[ | ]

function mode($arr) {
	$count_values = array_count_values($arr);
	$max = max($count_values);
	foreach( $count_values as $key => $count) if( $max == $count ) return $key;
}
$fruits = ['orange','banana','apple','orange','orange','apple'];
echo mode( $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 }}