When and how to use "." , ".$"and ".x", ".y" (pipe)

gapminder %>%
    nest(-country) %>%
    pwalk(~write_csv(x = .y, path = paste0(.x, ".csv") ) )

I've been using the tidyverse and using the dot symbol sensibly.
I had been passing lists, columns, etc. I'm not sure why it was working, but I was getting over it by trial and error anyway.

However, when I looked at this code, it specifies the columns by x,y.
This code is . . $data or . $country will not work.

How can I figure out how to use the dot symbol differently?

When "." and When should I use ".$col" and when should I use ".x"?

I'd like to know your advice and references.

thank you

It's for passing more than one param into a function

1 Like
gapminder %>%
    nest(-country) %>%
    pwalk(~write_csv(x = .$data, path = paste0(.$country, ".csv") ) )
gapminder %>%
  group_by(country) %>% 
  nest()%>%
  pwalk(list(.x = .$country,.y=.$data),~write_csv(x = .y,file= paste0(.x, ".csv") ) )

Thanks for the link.
I've just read it.
I couldn't figure out when and which symbols to use to be successful.
why can't Success above code?

The code below will succeed

data.frame(mean=0,sd=1) %>% 
map2(.x=.$mean,.y=.$sd,rnorm(.x,.y),n=10)

I think your confusion is because of how pwalk (& pmap etc) is passing the data into the function, especially after the nest call. They all pass a single list into the .f argument, unlike map2 etc, so when you use a ~ function the .f arguments for the two are:

function(..., .x = ..1, .y = ..2, . = ..1) {
    # do something here
}

You're also currently trying to use the '.' to reference the data frame within the called function (after the ~) which won't work as you now have a different context - in a ~ function the '.' now refers to the first argument, which walks across the unique values of 'country' in your pwalk example. You could learn more about the new context by running:

data.frame(mean=c(0),sd=c(1)) %>% 
map2(.x=.$mean,.y=.$sd,~{ 
                   browser()
            }, n=10)

and

gapminder %>%
            group_by(country) %>%
            nest() %>%
            pwalk(~{ 
                   browser()
            })

and these will probably work:

gapminder %>%
            group_by(country) %>%
            nest() %>%
            pwalk(~write_csv(list(...)$data, paste0(list(...)$country, ".csv")))
gapminder %>%
            group_by(country) %>%
            nest() %>%
            pwalk(~write_csv(tibble(...)$data, paste0(tibble(...)$country[1], ".csv")))
gapminder %>%
            group_by(country) %>%
            nest() %>%
            pwalk(~write_csv(.y$data, paste0(.x, ".csv")))
gapminder %>%
            group_by(country) %>%
            nest() %>%
            pwalk(~write_csv(..2$data, paste0(..1, ".csv")))
gapminder %>%
    group_by(country) %>%
    nest() %>%
    select(data, country) %>%
    pwalk(~write_csv(x = . , path = paste0(.y, ".csv") ) 
gapminder %>%
            group_by(country) %>%
            nest() %>%
            pwalk(function(country, data, ...) {
                             write_csv(data, paste0(country, ".csv"))
                         })

I have not tested these, & they may not all work, but it'll give you some idea how to troubleshoot these things in future. Hope it helps.

Edit: now tested & errors fixed!

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.