Tidyr function gather and paste inside loop











up vote
1
down vote

favorite












I'm trying to run a loop with multiple dataframes. I'm using the gather function from tidyr and I want to use as argument the index of the loop, i, along with a word, deaths.



I've been trying:



gather(data[[i]], "year", paste("deaths", i, sep="_"), 2:ncol(data[[i]]))



However, everytime I try that, it returns "Error: Must supply a symbol or a string as argument".



I read somewhere that tidyr evaluates things in a non-standard way and that the alternative is gather_, which uses standard evaluation.



However, the command



gather_(data[[i]], "year", paste("deaths", i, sep="_"), 2:ncol(data[[i]]))



Returns Error: Only strings can be converted to symbols.



However, I tought the paste command was already resulting in a string.



Anyone knows a fix?



Here is the full error:



"<error>
message: Only strings can be converted to symbols
class: `rlang_error`
backtrace:
-tidyr::gather_(...)
-tidyr:::gather_.data.frame(...)
-rlang::syms(gather_cols)
-rlang:::map(x, sym)
-base::lapply(.x, .f, ...)
-rlang:::FUN(X[[i]], ...)
Call `summary(rlang::last_error())` to see the full backtrace"


The full code:



require(datasus)
require(tidyr)

data_list <- list()

for(i in 1:2){
data_list[[i]] <- sim_inf10_mun(linha = "Município", coluna = "Ano do Óbito", periodo = c(1996:2016), municipio = "all",
capitulo_cid10 = i)
data_list[[i]] <- data.frame(data_list[i])
data_list[[i]] <- data_list[[i]][-1,]
data_list[[i]] <- data_list[[i]][,-ncol(data_list[[i]])]
data_list[[i]] <- gather(data_list[[i]], "ano", "deaths_01_i", 2:ncol(data_list[[i]]))
names(data_list[[i]])[1]<-"cod_mun"
data_list[[i]] <- transform(data_list[[i]], cod_mun = substr(cod_mun, 1, 6))
data_list[[i]] <- transform(data_list[[i]], ano = substr(ano, 2, 5))
}


This returns a panel dataset exactly the way I want, with (municipality code-year) identification in lines, and a value. My problem is that the value (column) name is "deaths_01_i", which is kinda obvious since it is quotation marks, whereas I wanted it to run with the loop. Thus I tried to implement it with a paste.



I know I can just change the variable name by adding a line names(data_list[[i]])[3]<-paste("deaths_01",i,sep="_"), but the problem got my attention to improving my understanding of the code.



Some words are in Portuguese but they are irrelevant to the problem. I also changed the range of the loop to avoid time issues.










share|improve this question




















  • 2




    Providing same sample data and desired output would increase the chances for an actual solution.
    – Wimpel
    Nov 9 at 16:17






  • 2




    Please provide sample code, the problem is elsewhere: functional programming + tidyverse + for loop = you are doing something very wrong.
    – JohnCoene
    Nov 9 at 16:19










  • @JohnCoene thanks for the reply, I've provided the sample code. The data is downloaded directly using the code.
    – jpugliese
    Nov 9 at 16:28










  • @Wimpel thanks for the reply, I've provided the sample code. The data is downloaded directly using the code.
    – jpugliese
    Nov 9 at 16:28










  • try add double "!" before your paste(), such like gather(data[[i]], "year", !!paste("deaths", i, sep="_"), 2:ncol(data[[i]]))
    – Darren Tsai
    Nov 9 at 17:03















up vote
1
down vote

favorite












I'm trying to run a loop with multiple dataframes. I'm using the gather function from tidyr and I want to use as argument the index of the loop, i, along with a word, deaths.



I've been trying:



gather(data[[i]], "year", paste("deaths", i, sep="_"), 2:ncol(data[[i]]))



However, everytime I try that, it returns "Error: Must supply a symbol or a string as argument".



I read somewhere that tidyr evaluates things in a non-standard way and that the alternative is gather_, which uses standard evaluation.



However, the command



gather_(data[[i]], "year", paste("deaths", i, sep="_"), 2:ncol(data[[i]]))



Returns Error: Only strings can be converted to symbols.



However, I tought the paste command was already resulting in a string.



Anyone knows a fix?



Here is the full error:



"<error>
message: Only strings can be converted to symbols
class: `rlang_error`
backtrace:
-tidyr::gather_(...)
-tidyr:::gather_.data.frame(...)
-rlang::syms(gather_cols)
-rlang:::map(x, sym)
-base::lapply(.x, .f, ...)
-rlang:::FUN(X[[i]], ...)
Call `summary(rlang::last_error())` to see the full backtrace"


The full code:



require(datasus)
require(tidyr)

data_list <- list()

for(i in 1:2){
data_list[[i]] <- sim_inf10_mun(linha = "Município", coluna = "Ano do Óbito", periodo = c(1996:2016), municipio = "all",
capitulo_cid10 = i)
data_list[[i]] <- data.frame(data_list[i])
data_list[[i]] <- data_list[[i]][-1,]
data_list[[i]] <- data_list[[i]][,-ncol(data_list[[i]])]
data_list[[i]] <- gather(data_list[[i]], "ano", "deaths_01_i", 2:ncol(data_list[[i]]))
names(data_list[[i]])[1]<-"cod_mun"
data_list[[i]] <- transform(data_list[[i]], cod_mun = substr(cod_mun, 1, 6))
data_list[[i]] <- transform(data_list[[i]], ano = substr(ano, 2, 5))
}


This returns a panel dataset exactly the way I want, with (municipality code-year) identification in lines, and a value. My problem is that the value (column) name is "deaths_01_i", which is kinda obvious since it is quotation marks, whereas I wanted it to run with the loop. Thus I tried to implement it with a paste.



I know I can just change the variable name by adding a line names(data_list[[i]])[3]<-paste("deaths_01",i,sep="_"), but the problem got my attention to improving my understanding of the code.



Some words are in Portuguese but they are irrelevant to the problem. I also changed the range of the loop to avoid time issues.










share|improve this question




















  • 2




    Providing same sample data and desired output would increase the chances for an actual solution.
    – Wimpel
    Nov 9 at 16:17






  • 2




    Please provide sample code, the problem is elsewhere: functional programming + tidyverse + for loop = you are doing something very wrong.
    – JohnCoene
    Nov 9 at 16:19










  • @JohnCoene thanks for the reply, I've provided the sample code. The data is downloaded directly using the code.
    – jpugliese
    Nov 9 at 16:28










  • @Wimpel thanks for the reply, I've provided the sample code. The data is downloaded directly using the code.
    – jpugliese
    Nov 9 at 16:28










  • try add double "!" before your paste(), such like gather(data[[i]], "year", !!paste("deaths", i, sep="_"), 2:ncol(data[[i]]))
    – Darren Tsai
    Nov 9 at 17:03













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm trying to run a loop with multiple dataframes. I'm using the gather function from tidyr and I want to use as argument the index of the loop, i, along with a word, deaths.



I've been trying:



gather(data[[i]], "year", paste("deaths", i, sep="_"), 2:ncol(data[[i]]))



However, everytime I try that, it returns "Error: Must supply a symbol or a string as argument".



I read somewhere that tidyr evaluates things in a non-standard way and that the alternative is gather_, which uses standard evaluation.



However, the command



gather_(data[[i]], "year", paste("deaths", i, sep="_"), 2:ncol(data[[i]]))



Returns Error: Only strings can be converted to symbols.



However, I tought the paste command was already resulting in a string.



Anyone knows a fix?



Here is the full error:



"<error>
message: Only strings can be converted to symbols
class: `rlang_error`
backtrace:
-tidyr::gather_(...)
-tidyr:::gather_.data.frame(...)
-rlang::syms(gather_cols)
-rlang:::map(x, sym)
-base::lapply(.x, .f, ...)
-rlang:::FUN(X[[i]], ...)
Call `summary(rlang::last_error())` to see the full backtrace"


The full code:



require(datasus)
require(tidyr)

data_list <- list()

for(i in 1:2){
data_list[[i]] <- sim_inf10_mun(linha = "Município", coluna = "Ano do Óbito", periodo = c(1996:2016), municipio = "all",
capitulo_cid10 = i)
data_list[[i]] <- data.frame(data_list[i])
data_list[[i]] <- data_list[[i]][-1,]
data_list[[i]] <- data_list[[i]][,-ncol(data_list[[i]])]
data_list[[i]] <- gather(data_list[[i]], "ano", "deaths_01_i", 2:ncol(data_list[[i]]))
names(data_list[[i]])[1]<-"cod_mun"
data_list[[i]] <- transform(data_list[[i]], cod_mun = substr(cod_mun, 1, 6))
data_list[[i]] <- transform(data_list[[i]], ano = substr(ano, 2, 5))
}


This returns a panel dataset exactly the way I want, with (municipality code-year) identification in lines, and a value. My problem is that the value (column) name is "deaths_01_i", which is kinda obvious since it is quotation marks, whereas I wanted it to run with the loop. Thus I tried to implement it with a paste.



I know I can just change the variable name by adding a line names(data_list[[i]])[3]<-paste("deaths_01",i,sep="_"), but the problem got my attention to improving my understanding of the code.



Some words are in Portuguese but they are irrelevant to the problem. I also changed the range of the loop to avoid time issues.










share|improve this question















I'm trying to run a loop with multiple dataframes. I'm using the gather function from tidyr and I want to use as argument the index of the loop, i, along with a word, deaths.



I've been trying:



gather(data[[i]], "year", paste("deaths", i, sep="_"), 2:ncol(data[[i]]))



However, everytime I try that, it returns "Error: Must supply a symbol or a string as argument".



I read somewhere that tidyr evaluates things in a non-standard way and that the alternative is gather_, which uses standard evaluation.



However, the command



gather_(data[[i]], "year", paste("deaths", i, sep="_"), 2:ncol(data[[i]]))



Returns Error: Only strings can be converted to symbols.



However, I tought the paste command was already resulting in a string.



Anyone knows a fix?



Here is the full error:



"<error>
message: Only strings can be converted to symbols
class: `rlang_error`
backtrace:
-tidyr::gather_(...)
-tidyr:::gather_.data.frame(...)
-rlang::syms(gather_cols)
-rlang:::map(x, sym)
-base::lapply(.x, .f, ...)
-rlang:::FUN(X[[i]], ...)
Call `summary(rlang::last_error())` to see the full backtrace"


The full code:



require(datasus)
require(tidyr)

data_list <- list()

for(i in 1:2){
data_list[[i]] <- sim_inf10_mun(linha = "Município", coluna = "Ano do Óbito", periodo = c(1996:2016), municipio = "all",
capitulo_cid10 = i)
data_list[[i]] <- data.frame(data_list[i])
data_list[[i]] <- data_list[[i]][-1,]
data_list[[i]] <- data_list[[i]][,-ncol(data_list[[i]])]
data_list[[i]] <- gather(data_list[[i]], "ano", "deaths_01_i", 2:ncol(data_list[[i]]))
names(data_list[[i]])[1]<-"cod_mun"
data_list[[i]] <- transform(data_list[[i]], cod_mun = substr(cod_mun, 1, 6))
data_list[[i]] <- transform(data_list[[i]], ano = substr(ano, 2, 5))
}


This returns a panel dataset exactly the way I want, with (municipality code-year) identification in lines, and a value. My problem is that the value (column) name is "deaths_01_i", which is kinda obvious since it is quotation marks, whereas I wanted it to run with the loop. Thus I tried to implement it with a paste.



I know I can just change the variable name by adding a line names(data_list[[i]])[3]<-paste("deaths_01",i,sep="_"), but the problem got my attention to improving my understanding of the code.



Some words are in Portuguese but they are irrelevant to the problem. I also changed the range of the loop to avoid time issues.







r tidyr






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 16:57

























asked Nov 9 at 16:02









jpugliese

185




185








  • 2




    Providing same sample data and desired output would increase the chances for an actual solution.
    – Wimpel
    Nov 9 at 16:17






  • 2




    Please provide sample code, the problem is elsewhere: functional programming + tidyverse + for loop = you are doing something very wrong.
    – JohnCoene
    Nov 9 at 16:19










  • @JohnCoene thanks for the reply, I've provided the sample code. The data is downloaded directly using the code.
    – jpugliese
    Nov 9 at 16:28










  • @Wimpel thanks for the reply, I've provided the sample code. The data is downloaded directly using the code.
    – jpugliese
    Nov 9 at 16:28










  • try add double "!" before your paste(), such like gather(data[[i]], "year", !!paste("deaths", i, sep="_"), 2:ncol(data[[i]]))
    – Darren Tsai
    Nov 9 at 17:03














  • 2




    Providing same sample data and desired output would increase the chances for an actual solution.
    – Wimpel
    Nov 9 at 16:17






  • 2




    Please provide sample code, the problem is elsewhere: functional programming + tidyverse + for loop = you are doing something very wrong.
    – JohnCoene
    Nov 9 at 16:19










  • @JohnCoene thanks for the reply, I've provided the sample code. The data is downloaded directly using the code.
    – jpugliese
    Nov 9 at 16:28










  • @Wimpel thanks for the reply, I've provided the sample code. The data is downloaded directly using the code.
    – jpugliese
    Nov 9 at 16:28










  • try add double "!" before your paste(), such like gather(data[[i]], "year", !!paste("deaths", i, sep="_"), 2:ncol(data[[i]]))
    – Darren Tsai
    Nov 9 at 17:03








2




2




Providing same sample data and desired output would increase the chances for an actual solution.
– Wimpel
Nov 9 at 16:17




Providing same sample data and desired output would increase the chances for an actual solution.
– Wimpel
Nov 9 at 16:17




2




2




Please provide sample code, the problem is elsewhere: functional programming + tidyverse + for loop = you are doing something very wrong.
– JohnCoene
Nov 9 at 16:19




Please provide sample code, the problem is elsewhere: functional programming + tidyverse + for loop = you are doing something very wrong.
– JohnCoene
Nov 9 at 16:19












@JohnCoene thanks for the reply, I've provided the sample code. The data is downloaded directly using the code.
– jpugliese
Nov 9 at 16:28




@JohnCoene thanks for the reply, I've provided the sample code. The data is downloaded directly using the code.
– jpugliese
Nov 9 at 16:28












@Wimpel thanks for the reply, I've provided the sample code. The data is downloaded directly using the code.
– jpugliese
Nov 9 at 16:28




@Wimpel thanks for the reply, I've provided the sample code. The data is downloaded directly using the code.
– jpugliese
Nov 9 at 16:28












try add double "!" before your paste(), such like gather(data[[i]], "year", !!paste("deaths", i, sep="_"), 2:ncol(data[[i]]))
– Darren Tsai
Nov 9 at 17:03




try add double "!" before your paste(), such like gather(data[[i]], "year", !!paste("deaths", i, sep="_"), 2:ncol(data[[i]]))
– Darren Tsai
Nov 9 at 17:03

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53229256%2ftidyr-function-gather-and-paste-inside-loop%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53229256%2ftidyr-function-gather-and-paste-inside-loop%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()