2016-04-03 3 views
1

tidyr::nestによって作成されたテーブルのグループでpurr::mapを使用してmutate_eachを実行しようとしています。以下はtidyrネストしたテーブルのmutate_eachが "Unknown inputs"を返します

は私がしようとしているものの例と結果のエラーです:

library(tidyr) 
library(gapminder) 
library(dplyr) 
library(purrr) 


by_country <- gapminder %>% 
    group_by(continent, country) %>% 
    nest() %>% 
    mutate(data2 = map(data, ~ mutate_each(.,funs(as.numeric)))) 

ができます:

Error: Unknown inputs

をこの問題の原因は何?修正はありますか?私はmutate_eachの代わりにapplyを使うことができましたが、それはrownamesを削除します。

編集:最初の応答から、目的がはっきりしていないようです。だから、おそらく私は間違ったやり方をしようとしています。私に説明させてください。

そうした場合:

by_country <- gapminder %>% 
    group_by(continent, country) %>% 
    nest() 

あなたが持っている:

by_country$data[[1]] 
Source: local data frame [12 x 4] 

    year lifeExp  pop gdpPercap 
    (int) (dbl) (int)  (dbl) 
1 1952 28.801 8425333 779.4453 
2 1957 30.332 9240934 820.8530 
3 1962 31.997 10267083 853.1007 
4 1967 34.020 11537966 836.1971 
5 1972 36.088 13079460 739.9811 
6 1977 38.438 14880372 786.1134 
7 1982 39.854 12881816 978.0114 
8 1987 40.822 13867957 852.3959 
9 1992 41.674 16317921 649.3414 
10 1997 41.763 22227415 635.3414 
11 2002 42.129 25268405 726.7341 
12 2007 43.828 31889923 974.5803 

私は各列の関数を実行されたいです。各ネストした表に対して個別に実行されます。だから私は、各テーブルの上に以下のequevalentを実行したいと思います:

mutate_each(by_country$data[[1]],funs(as.numeric)) 

与える:

Source: local data frame [12 x 4] 

    year lifeExp  pop gdpPercap 
    (dbl) (dbl) (dbl)  (dbl) 
1 1952 28.801 8425333 779.4453 
2 1957 30.332 9240934 820.8530 
3 1962 31.997 10267083 853.1007 
4 1967 34.020 11537966 836.1971 
5 1972 36.088 13079460 739.9811 
6 1977 38.438 14880372 786.1134 
7 1982 39.854 12881816 978.0114 
8 1987 40.822 13867957 852.3959 
9 1992 41.674 16317921 649.3414 
10 1997 41.763 22227415 635.3414 
11 2002 42.129 25268405 726.7341 
12 2007 43.828 31889923 974.5803 
+1

達成しようとしていることは何ですか?おそらく 'gapminder'データフレームが入力として与えられると期待される出力を表示するでしょうか? – Gopala

+0

OK。質問が更新されました。 –

+0

はなぜこれをしない: 'gapminder%>%mutate_each_(低速運行(as.numeric)、名前(gapminder)[sapply(gapminder、is.integer)])%>%GROUP_BY(国、大陸)%>%巣() '? – Gopala

答えて

2

mapは、関数の引数は式として与えられたときに入力されたデータを参照するために.xを使用しています。その変更後、mutate_eachの形のための二つの可能性があります。

by_country <- gapminder %>% 
    group_by(continent, country) %>% 
    nest() %>% 
    mutate(data2 = map(data, ~ mutate_each(.x, "as.numeric"))) 

または

by_country2 <- gapminder %>% 
    group_by(continent, country) %>% 
    nest() %>% 
    mutate(data2 = map(data, ~ mutate_each(.x, funs(as.numeric(.))))) 

mutate(data2 = map(data, ~ mutate_each(.x, funs(as.numeric))))も動作するはずと思われるが、それはしません。

+0

素晴らしい!本当にありがとう。 –

関連する問題