R의 데이터 프레임에서 열의 가장 높은 값을 찾는 방법은 무엇입니까?
오존이라고 부르는 다음 데이터 프레임이 있습니다.
Ozone Solar.R Wind Temp Month Day
1 41 190 7.4 67 5 1
2 36 118 8.0 72 5 2
3 12 149 12.6 74 5 3
4 18 313 11.5 62 5 4
5 NA NA 14.3 56 5 5
6 28 NA 14.9 66 5 6
7 23 299 8.6 65 5 7
8 19 99 13.8 59 5 8
9 8 19 20.1 61 5 9
나는에서 가장 높은 값을 추출하고 싶습니다 ozone
, Solar.R
, Wind
...
또한 가능한 Solar.R
경우이 데이터 프레임의 열을 내림차순으로 정렬하는 방법
나는 시도했다
max(ozone, na.rm=T)
데이터 세트에서 가장 높은 가치를 제공합니다.
나는 또한 시도했다
max(subset(ozone,Ozone))
하지만 얻었다 "subset" must be logical."
다음 명령을 사용하여 각 열의 하위 집합을 보유하도록 개체를 설정할 수 있습니다.
ozone <- subset(ozone, Ozone >0)
max(ozone,na.rm=T)
그러나 열이 아닌 데이터 프레임의 최대 값인 334와 동일한 값을 제공합니다.
어떤 도움이라도 좋을 것입니다. 감사합니다.
유사하게 colMeans
, colSums
, 등, 당신이 열 최대 기능을 쓸 수 colMax
및 열 정렬 기능 colSort
.
colMax <- function(data) sapply(data, max, na.rm = TRUE)
colSort <- function(data, ...) sapply(data, sort, ...)
나는 ...
당신의 음모를 촉발시키기 위해 두 번째 기능에서 사용 합니다.
데이터 가져 오기 :
dat <- read.table(h=T, text = "Ozone Solar.R Wind Temp Month Day
1 41 190 7.4 67 5 1
2 36 118 8.0 72 5 2
3 12 149 12.6 74 5 3
4 18 313 11.5 62 5 4
5 NA NA 14.3 56 5 5
6 28 NA 14.9 66 5 6
7 23 299 8.6 65 5 7
8 19 99 13.8 59 5 8
9 8 19 20.1 61 5 9")
colMax
샘플 데이터에 함수 사용 :
colMax(dat)
# Ozone Solar.R Wind Temp Month Day
# 41.0 313.0 20.1 74.0 5.0 9.0
단일 열에서 정렬을 수행하려면
sort(dat$Solar.R, decreasing = TRUE)
# [1] 313 299 190 149 118 99 19
모든 열에서 colSort
함수를 사용 합니다.
colSort(dat, decreasing = TRUE) ## compare with '...' above
원하는 열의 최대 값을 얻으려면 다음과 같이하십시오.
max(ozone$Ozone, na.rm = TRUE)
모든 열의 최대 값을 얻으려면 다음을 원합니다.
apply(ozone, 2, function(x) max(x, na.rm = TRUE))
그리고 정렬 :
ozone[order(ozone$Solar.R),]
또는 다른 방향으로 정렬하려면 :
ozone[rev(order(ozone$Solar.R)),]
dplyr
해결책은 다음과 같습니다 .
library(dplyr)
# find max for each column
summarise_each(ozone, funs(max(., na.rm=TRUE)))
# sort by Solar.R, descending
arrange(ozone, desc(Solar.R))
업데이트 : summarise_each()
기능의보다 풍부한 기능의 가족에 찬성 중지되었습니다 : mutate_all()
, mutate_at()
, mutate_if()
, summarise_all()
, summarise_at()
,summarise_if()
방법은 다음과 같습니다.
# find max for each column
ozone %>%
summarise_if(is.numeric, funs(max(., na.rm=TRUE)))%>%
arrange(Ozone)
또는
ozone %>%
summarise_at(vars(1:6), funs(max(., na.rm=TRUE)))%>%
arrange(Ozone)
각 열의 최대 값을 찾는 데 대한 응답으로 다음 apply()
함수를 사용해 볼 수 있습니다.
> apply(ozone, MARGIN = 2, function(x) max(x, na.rm=TRUE))
Ozone Solar.R Wind Temp Month Day
41.0 313.0 20.1 74.0 5.0 9.0
또 다른 방법은? pmax를 사용하는 것입니다.
do.call('pmax', c(as.data.frame(t(ozone)),na.rm=TRUE))
#[1] 41.0 313.0 20.1 74.0 5.0 9.0
에서 데이터한다고 가정 data.frame
이라고는 maxinozone
,이 작업을 수행 할 수 있습니다
max(maxinozone[1, ], na.rm = TRUE)
max(ozone$Ozone, na.rm = TRUE)
should do the trick. Remember to include the na.rm = TRUE
or else R will return NA.
max(may$Ozone, na.rm = TRUE)
Without $Ozone
it will filter in the whole data frame, this can be learned in the swirl library.
I'm studying this course on Coursera too ~
Try this solution:
Oz<-subset(data, data$Month==5,select=Ozone) # select ozone value in the month of
#May (i.e. Month = 5)
summary(T) #gives caracteristics of table( contains 1 column of Ozone) including max, min ...
There is a package matrixStats
that provides some functions to do column and row summaries, see in the package vignette, but you have to convert your data.frame into a matrix.
Then you run: colMaxs(as.matrix(ozone))
ReferenceURL : https://stackoverflow.com/questions/24212739/how-to-find-the-highest-value-of-a-column-in-a-data-frame-in-r
'programing' 카테고리의 다른 글
데이터 프레임에서 마지막 요소의 액세스 인덱스 (0) | 2021.01.07 |
---|---|
FragmentPagerAdapter로 다시 이동-> 조각이 비어 있습니다. (0) | 2021.01.07 |
C가->와.을 구분하는 이유는 무엇입니까? (0) | 2021.01.06 |
앱 ID와 번들 ID의 차이점은 무엇입니까? (0) | 2021.01.06 |
스트리밍 서버에서 RTP와 RTSP의 차이점은 무엇입니까? (0) | 2021.01.06 |