2014-11-17 6 views
1

R에서 동일한 두 열에 대해 다른 값이 데이터 프레임의 서브 세트를 어떻게 :는 내가이 데이터 프레임이

dput (DF)를

structure(list(Metrics = c("db1.Tablespace_Space_Used_(%)", "db1.Tablespace_Space_Used_(%)", 
"db1.Tablespace_Space_Used_(%)", "db1.Tablespace_Space_Used_(%)", 
"db2.Tablespace_Space_Used_(%)", "db2.Tablespace_Space_Used_(%)", 
"db1.Tablespace_Space_Used_(%)", "db2.Tablespace_Space_Used_(%)", 
"db1.Tablespace_Space_Used_(%)", "db2.Tablespace_Space_Used_(%)", 
"db2.Tablespace_Space_Used_(%)", "db2.Tablespace_Space_Used_(%)", 
"db2.Tablespace_Space_Used_(%)", "db1.Tablespace_Space_Used_(%)", 
"db1.Tablespace_Space_Used_(%)", "db1.Tablespace_Space_Used_(%)", 
"db1.Tablespace_Space_Used_(%)", "db1.Tablespace_Space_Used_(%)", 
"db1.Tablespace_Space_Used_(%)", "db1.Tablespace_Space_Used_(%)" 
), Date = c(1416257563.98707, 1416257563.98707, 1416257563.98707, 
1416257563.98707, 1416257563.98707, 1416257563.98707, 1416257563.98707, 
1416257563.98707, 1416257563.98707, 1416257563.98707, 1416257563.98707, 
1416257563.98707, 1416257563.98707, 1416257563.98707, 1416257563.98707, 
1416257563.98707, 1416257563.98707, 1416257563.98707, 1416257563.98707, 
1416257563.98707), Value = c(0, 0.02, 0.01, 0, 0.01, 0.01, 0.07, 
0, 2.02, 0, 0, 9.32, 0.02, 9.27, 0, 12.72, 12.72, 12.72, 0.08, 
12.72), Type1 = c("type=rac_database", "type=rac_database", "type=rac_database", 
"type=rac_database", "type=rac_database", "type=rac_database", 
"type=rac_database", "type=rac_database", "type=rac_database", 
"type=rac_database", "type=rac_database", "type=rac_database", 
"type=rac_database", "type=rac_database", "type=rac_database", 
"type=rac_database", "type=rac_database", "type=rac_database", 
"type=rac_database", "type=rac_database")), .Names = c("Metrics", 
"Date", "Value", "Type1"), class = "data.frame", row.names = c(10092L, 
10097L, 10103L, 10104L, 10107L, 10108L, 10111L, 10112L, 10114L, 
10115L, 10116L, 10117L, 10118L, 10120L, 10121L, 10188L, 10189L, 
10190L, 10192L, 10216L)) 

이 큰 데이터의 하위 집합입니다 틀. 동일한 측정 항목 및 날짜에 대해 알 수 있듯이 여러 값이 있습니다. 동일한 데이터 및 측정 항목 유형에 대해 최대 값만 선택할 수 있기를 바랍니다. 따라서 동일한 날짜와 측정 항목에 대해서는 최대 값이어야하는 하나의 값만 가져야합니다. 어떤 아이디어, 어떻게이 DF의 하위 집합을 만들 수 있습니까?

예를 들어, 메트릭 :

db1.Tablespace_Space_Used_(%) 1416257564 12.72 type=rac_database 

답변

1

여기에 주어진 대답과 동일 답변 : Finding maximum value of one column (by group) and inserting value into another data frame in R

데이터 프레임을 가정하십시오. 전자가 호출됩니다 df

df_1 <- aggregate(Value ~ Metrics + Date + Type1, df, max) 
df_1 
#edit: removed 'cbind' 

출력

     Metrics  Date    Type1 Value 
1 db1.Tablespace_Space_Used_(%) 1416257564 type=rac_database 12.72 
2 db2.Tablespace_Space_Used_(%) 1416257564 type=rac_database 9.32 
0

이 방법에 대해 :

> # find the maximum for Value for each combination of Metrics and Date 
> df2 <- aggregate(df$Value, by=list(Metrics=df$Metrics, Date=df$Date), max) 
> colnames(df2)[3] <- "Value" 

> # add the corresponding value for Type1 
> df2$Type1 <- df[df$Metrics == df2$Metrics & df$Date == df2$Date & df$Value == df2$Value, "Type1"] 

> # result 
> df2 
         Metrics  Date Value    Type1 
1 db1.Tablespace_Space_Used_(%) 1416257564 12.72 type=rac_database 
2 db2.Tablespace_Space_Used_(%) 1416257564 9.32 type=rac_database 
내 안양에서 1416257564

, 나는 하나 개의 항목이 있어야합니다 db1.Tablespace_Space_Used _ (%) 및 날짜

관련 문제