2015-10-06 10 views
9

mogę użyć następujących zwrócić maksymalnie 2 kolumnachdplyr mutować rowwise max zakresu kolumn

newiris<-iris %>% 
rowwise() %>% 
mutate(mak=max(Sepal.Width,Petal.Length)) 

Co chcę zrobić, to okaże się, że maksymalnie w całym szeregu kolumn więc nie mam nazwać każdy taki jak ten

newiris<-iris %>% 
rowwise() %>% 
mutate(mak=max(Sepal.Width:Petal.Length)) 

Wszelkie pomysły?

Odpowiedz

16

Zamiast rowwise(), można to zrobić z pmax

iris %>% 
     mutate(mak=pmax(Sepal.Width,Petal.Length, Petal.Width)) 

Może być możemy użyć interp z library(lazyeval) jeśli chcemy odwołać się do nazwy kolumn przechowywane w vector.

library(lazyeval) 
nm1 <- names(iris)[2:4] 
iris %>% 
    mutate_(mak= interp(~pmax(v1), v1= as.name(nm1))) 
+1

dobry pomysł na pmax. Masz pomysł, jak znaleźć maksimum z 3 kolumn, odwołując się tylko do podpórek? np .: Sepal.Width przez Petal.Width? – user2502836

+0

@ user2502836 Zaktualizowano wpis. Sprawdź, czy to pomaga. – akrun

2

Aby wybrać kilka kolumn bez wpisywania całych nazw przy użyciu dplyr Wolę select parametr z subset funkcji.

można uzyskać pożądany efekt takiego:

iris %>% subset(select = 2:4) %>% mutate(mak = do.call(pmax, (.))) %>% 
    select(mak) %>% cbind(iris) 
+1

Myślę, że można po prostu zrobić 'select (2: 4)' zamiast 'subset (select = 2: 4)'. –

0

Wygląda @ akrun na odpowiedź odnosi się jedynie do przypadków, kiedy można wpisać nazwy wszystkich zmiennych, czy to za pomocą mutate bezpośrednio z mutate(pmax_value=pmax(var1, var2)) lub gdy za pomocą leniwej oceny z mutate_ i interp przez mutate_(interp(~pmax(v1, v2), v1=as.name(var1), v2=as.name(var2)).

Widzę dwa sposoby, aby to zrobić, jeśli chcesz użyć składni dwukropka Sepal.Length:Petal.Width lub jeśli masz wektor z nazwami kolumn.

Pierwsza jest bardziej elegancka. Po uporządkowaniu danych i uzyskaniu maksymalnych wartości po zgrupowaniu:

data(iris) 
library(dplyr) 
library(tidyr) 

iris_id = iris %>% mutate(id=1:nrow(.)) 
iris_id %>% 
    gather('attribute', 'value', Sepal.Length:Petal.Width) %>% 
    group_by(id) %>% 
    summarize(max_attribute=max(value)) %>% 
    right_join(iris_id, by='id') %>% 
    head(3) 
## # A tibble: 3 × 7 
##  id max_attribute Sepal.Length Sepal.Width Petal.Length Petal.Width Species 
## <int>   <dbl>  <dbl>  <dbl>  <dbl>  <dbl> <fctr> 
## 1  1   5.1   5.1   3.5   1.4   0.2 setosa 
## 2  2   4.9   4.9   3.0   1.4   0.2 setosa 
## 3  3   4.7   4.7   3.2   1.3   0.2 setosa 

Najtrudniejszym sposobem jest użycie interpolowanej formuły. Jest to dobre, jeśli masz wektor znaków z nazwami zmiennych, które mają zostać przekroczone lub jeśli stół jest zbyt wysoki/szeroki, aby można go było uporządkować.

# Make a character vector of the names of the columns we want to take the 
# maximum over 
target_columns = iris %>% select(-Species) %>% names 
## [1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" 

# Make a vector of dummy variables that will take the place of the real 
# column names inside the interpolated formula 
dummy_vars = sapply(1:length(target_columns), function(i) sprintf('x%i', i)) 
## [1] "x1" "x2" "x3" "x4" 

# Paste those variables together to make the argument of the pmax in the 
# interpolated formula 
dummy_vars_string = paste0(dummy_vars, collapse=',') 
## [1] "x1,x2,x3,x4" 

# Make a named list that maps the dummy variable names (e.g., x1) to the 
# real variable names (e.g., Sepal.Length) 
dummy_vars_list = lapply(target_columns, as.name) %>% setNames(dummy_vars) 
## $x1 
## Sepal.Length 
## 
## $x2 
## Sepal.Width 
## 
## $x3 
## Petal.Length 
## 
## $x4 
## Petal.Width 

# Make a pmax formula using the dummy variables 
max_formula = as.formula(paste0(c('~pmax(', dummy_vars_string, ')'), collapse='')) 
## ~pmax(x1, x2, x3, x4) 

# Interpolate the formula using the named variables 
library(lazyeval) 
iris %>% 
    mutate_(max_attribute=interp(max_formula, .values=dummy_vars_list)) %>% 
    head(3) 
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species max_attribute 
## 1   5.1   3.5   1.4   0.2 setosa   5.1 
## 2   4.9   3.0   1.4   0.2 setosa   4.9 
## 3   4.7   3.2   1.3   0.2 setosa   4.7 
Powiązane problemy