function works on one element of list, doesn't work on full list, R





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm trying to make a function to add tags to a data frame.
The name of the data frame contains the information I need (date, selection, treatment, etc...). So I made a function that extracts the information I need. I have a large list containing all the data frames, and when I apply the function to the list, it does create the new columns for the tags but the values are NA-s. Every data frame has the same name structure, and if I extract a data frame from the list and run the function it works. Can you help me find out why doesn't it work when I apply it to the list?



here is my function :



library(stringr)

tagging <- function(H){

namey<-deparse(substitute(H)) #get the name of the data frame
namey<- str_sub(namey,1, -5) #drop the .csv
H$date<-substring(namey,1, 6) # get the first 6 characters containing the date
H$selection<- word(namey, -1) #get the last word
H$treatment<- word(namey, -2) # get the second last word
H$REP<- word(namey, -3) # get the third last word
return(H)
}


And I apply it like this



 ListofData.tagged<-lapply(ListofData, tagging)


The name of the data frames looks like this:



180503 xyz1-6 R4_A6_xyz 5 yes.csv









share|improve this question




















  • 4





    Could you prove a minimal example of what ListofData contains?

    – Anders Ellern Bilgrau
    Nov 16 '18 at 18:08











  • Looks like it is the file name instead of the object name. I think you need to loop through the names of the ListofData?

    – akrun
    Nov 16 '18 at 18:10













  • Try map(names(ListofDatalst), ~ str_sub(.x, 1, -5))

    – akrun
    Nov 16 '18 at 18:15











  • The problem is a lapply problem: it passes X[[1]], then X[[2]], etc to the function. Put cat("namey:", namey, "n") right after deparse/substitute to see what is the df name in the function.

    – Rui Barradas
    Nov 16 '18 at 18:17











  • Anders Ellern Bilgrau : ListofData contains 300 data frames. Each dataframe has the same structure, same variables and the same "structure" for the name. akrun : the name of the file is the name of the object, because I import the .csv files as follows : temp = list.files(pattern="*.csv") ListofData = lapply(temp, read.csv)

    – user163731
    Nov 16 '18 at 18:19


















0















I'm trying to make a function to add tags to a data frame.
The name of the data frame contains the information I need (date, selection, treatment, etc...). So I made a function that extracts the information I need. I have a large list containing all the data frames, and when I apply the function to the list, it does create the new columns for the tags but the values are NA-s. Every data frame has the same name structure, and if I extract a data frame from the list and run the function it works. Can you help me find out why doesn't it work when I apply it to the list?



here is my function :



library(stringr)

tagging <- function(H){

namey<-deparse(substitute(H)) #get the name of the data frame
namey<- str_sub(namey,1, -5) #drop the .csv
H$date<-substring(namey,1, 6) # get the first 6 characters containing the date
H$selection<- word(namey, -1) #get the last word
H$treatment<- word(namey, -2) # get the second last word
H$REP<- word(namey, -3) # get the third last word
return(H)
}


And I apply it like this



 ListofData.tagged<-lapply(ListofData, tagging)


The name of the data frames looks like this:



180503 xyz1-6 R4_A6_xyz 5 yes.csv









share|improve this question




















  • 4





    Could you prove a minimal example of what ListofData contains?

    – Anders Ellern Bilgrau
    Nov 16 '18 at 18:08











  • Looks like it is the file name instead of the object name. I think you need to loop through the names of the ListofData?

    – akrun
    Nov 16 '18 at 18:10













  • Try map(names(ListofDatalst), ~ str_sub(.x, 1, -5))

    – akrun
    Nov 16 '18 at 18:15











  • The problem is a lapply problem: it passes X[[1]], then X[[2]], etc to the function. Put cat("namey:", namey, "n") right after deparse/substitute to see what is the df name in the function.

    – Rui Barradas
    Nov 16 '18 at 18:17











  • Anders Ellern Bilgrau : ListofData contains 300 data frames. Each dataframe has the same structure, same variables and the same "structure" for the name. akrun : the name of the file is the name of the object, because I import the .csv files as follows : temp = list.files(pattern="*.csv") ListofData = lapply(temp, read.csv)

    – user163731
    Nov 16 '18 at 18:19














0












0








0








I'm trying to make a function to add tags to a data frame.
The name of the data frame contains the information I need (date, selection, treatment, etc...). So I made a function that extracts the information I need. I have a large list containing all the data frames, and when I apply the function to the list, it does create the new columns for the tags but the values are NA-s. Every data frame has the same name structure, and if I extract a data frame from the list and run the function it works. Can you help me find out why doesn't it work when I apply it to the list?



here is my function :



library(stringr)

tagging <- function(H){

namey<-deparse(substitute(H)) #get the name of the data frame
namey<- str_sub(namey,1, -5) #drop the .csv
H$date<-substring(namey,1, 6) # get the first 6 characters containing the date
H$selection<- word(namey, -1) #get the last word
H$treatment<- word(namey, -2) # get the second last word
H$REP<- word(namey, -3) # get the third last word
return(H)
}


And I apply it like this



 ListofData.tagged<-lapply(ListofData, tagging)


The name of the data frames looks like this:



180503 xyz1-6 R4_A6_xyz 5 yes.csv









share|improve this question
















I'm trying to make a function to add tags to a data frame.
The name of the data frame contains the information I need (date, selection, treatment, etc...). So I made a function that extracts the information I need. I have a large list containing all the data frames, and when I apply the function to the list, it does create the new columns for the tags but the values are NA-s. Every data frame has the same name structure, and if I extract a data frame from the list and run the function it works. Can you help me find out why doesn't it work when I apply it to the list?



here is my function :



library(stringr)

tagging <- function(H){

namey<-deparse(substitute(H)) #get the name of the data frame
namey<- str_sub(namey,1, -5) #drop the .csv
H$date<-substring(namey,1, 6) # get the first 6 characters containing the date
H$selection<- word(namey, -1) #get the last word
H$treatment<- word(namey, -2) # get the second last word
H$REP<- word(namey, -3) # get the third last word
return(H)
}


And I apply it like this



 ListofData.tagged<-lapply(ListofData, tagging)


The name of the data frames looks like this:



180503 xyz1-6 R4_A6_xyz 5 yes.csv






r list function user-defined-functions lapply






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 18:11









Rui Barradas

18.3k51833




18.3k51833










asked Nov 16 '18 at 18:06









user163731user163731

61




61








  • 4





    Could you prove a minimal example of what ListofData contains?

    – Anders Ellern Bilgrau
    Nov 16 '18 at 18:08











  • Looks like it is the file name instead of the object name. I think you need to loop through the names of the ListofData?

    – akrun
    Nov 16 '18 at 18:10













  • Try map(names(ListofDatalst), ~ str_sub(.x, 1, -5))

    – akrun
    Nov 16 '18 at 18:15











  • The problem is a lapply problem: it passes X[[1]], then X[[2]], etc to the function. Put cat("namey:", namey, "n") right after deparse/substitute to see what is the df name in the function.

    – Rui Barradas
    Nov 16 '18 at 18:17











  • Anders Ellern Bilgrau : ListofData contains 300 data frames. Each dataframe has the same structure, same variables and the same "structure" for the name. akrun : the name of the file is the name of the object, because I import the .csv files as follows : temp = list.files(pattern="*.csv") ListofData = lapply(temp, read.csv)

    – user163731
    Nov 16 '18 at 18:19














  • 4





    Could you prove a minimal example of what ListofData contains?

    – Anders Ellern Bilgrau
    Nov 16 '18 at 18:08











  • Looks like it is the file name instead of the object name. I think you need to loop through the names of the ListofData?

    – akrun
    Nov 16 '18 at 18:10













  • Try map(names(ListofDatalst), ~ str_sub(.x, 1, -5))

    – akrun
    Nov 16 '18 at 18:15











  • The problem is a lapply problem: it passes X[[1]], then X[[2]], etc to the function. Put cat("namey:", namey, "n") right after deparse/substitute to see what is the df name in the function.

    – Rui Barradas
    Nov 16 '18 at 18:17











  • Anders Ellern Bilgrau : ListofData contains 300 data frames. Each dataframe has the same structure, same variables and the same "structure" for the name. akrun : the name of the file is the name of the object, because I import the .csv files as follows : temp = list.files(pattern="*.csv") ListofData = lapply(temp, read.csv)

    – user163731
    Nov 16 '18 at 18:19








4




4





Could you prove a minimal example of what ListofData contains?

– Anders Ellern Bilgrau
Nov 16 '18 at 18:08





Could you prove a minimal example of what ListofData contains?

– Anders Ellern Bilgrau
Nov 16 '18 at 18:08













Looks like it is the file name instead of the object name. I think you need to loop through the names of the ListofData?

– akrun
Nov 16 '18 at 18:10







Looks like it is the file name instead of the object name. I think you need to loop through the names of the ListofData?

– akrun
Nov 16 '18 at 18:10















Try map(names(ListofDatalst), ~ str_sub(.x, 1, -5))

– akrun
Nov 16 '18 at 18:15





Try map(names(ListofDatalst), ~ str_sub(.x, 1, -5))

– akrun
Nov 16 '18 at 18:15













The problem is a lapply problem: it passes X[[1]], then X[[2]], etc to the function. Put cat("namey:", namey, "n") right after deparse/substitute to see what is the df name in the function.

– Rui Barradas
Nov 16 '18 at 18:17





The problem is a lapply problem: it passes X[[1]], then X[[2]], etc to the function. Put cat("namey:", namey, "n") right after deparse/substitute to see what is the df name in the function.

– Rui Barradas
Nov 16 '18 at 18:17













Anders Ellern Bilgrau : ListofData contains 300 data frames. Each dataframe has the same structure, same variables and the same "structure" for the name. akrun : the name of the file is the name of the object, because I import the .csv files as follows : temp = list.files(pattern="*.csv") ListofData = lapply(temp, read.csv)

– user163731
Nov 16 '18 at 18:19





Anders Ellern Bilgrau : ListofData contains 300 data frames. Each dataframe has the same structure, same variables and the same "structure" for the name. akrun : the name of the file is the name of the object, because I import the .csv files as follows : temp = list.files(pattern="*.csv") ListofData = lapply(temp, read.csv)

– user163731
Nov 16 '18 at 18:19












1 Answer
1






active

oldest

votes


















0














Import this way so you keep your names:



library(tidyverse)
ListofData <- map(set_names(temp), read_csv)


then we modify your function to add the name as a second parameter, which will be used through imap, so we get rid of the first line too :



tagging <- function(H, namey){
namey<- str_sub(namey,1, -5) #drop the .csv
H$date<-substring(namey,1, 6) # get the first 6 characters containing the date
H$selection<- word(namey, -1) #get the last word
H$treatment<- word(namey, -2) # get the second last word
H$REP<- word(namey, -3) # get the third last word
return(H)
}


Then imap passes the data to the H argument and the element names to the namey argument.



ListofData.tagged <- imap(ListofData, tagging)




base R translation



ListofData <- lapply(setNames(temp,temp), read.csv)
ListofData.tagged <- Map(tagging, ListofData, names(ListofData))




Or if you don't care about naming the elements of ListofData you can do directly Map(tagging, ListofData, temp) (still keeping the new definition of tagging).






share|improve this answer


























  • thanks @rui-barradas

    – Moody_Mudskipper
    Nov 16 '18 at 18:57











  • It works!!! Thank you so so so much!

    – user163731
    Nov 17 '18 at 14:08












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',
autoActivateHeartbeat: false,
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%2f53343205%2ffunction-works-on-one-element-of-list-doesnt-work-on-full-list-r%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Import this way so you keep your names:



library(tidyverse)
ListofData <- map(set_names(temp), read_csv)


then we modify your function to add the name as a second parameter, which will be used through imap, so we get rid of the first line too :



tagging <- function(H, namey){
namey<- str_sub(namey,1, -5) #drop the .csv
H$date<-substring(namey,1, 6) # get the first 6 characters containing the date
H$selection<- word(namey, -1) #get the last word
H$treatment<- word(namey, -2) # get the second last word
H$REP<- word(namey, -3) # get the third last word
return(H)
}


Then imap passes the data to the H argument and the element names to the namey argument.



ListofData.tagged <- imap(ListofData, tagging)




base R translation



ListofData <- lapply(setNames(temp,temp), read.csv)
ListofData.tagged <- Map(tagging, ListofData, names(ListofData))




Or if you don't care about naming the elements of ListofData you can do directly Map(tagging, ListofData, temp) (still keeping the new definition of tagging).






share|improve this answer


























  • thanks @rui-barradas

    – Moody_Mudskipper
    Nov 16 '18 at 18:57











  • It works!!! Thank you so so so much!

    – user163731
    Nov 17 '18 at 14:08
















0














Import this way so you keep your names:



library(tidyverse)
ListofData <- map(set_names(temp), read_csv)


then we modify your function to add the name as a second parameter, which will be used through imap, so we get rid of the first line too :



tagging <- function(H, namey){
namey<- str_sub(namey,1, -5) #drop the .csv
H$date<-substring(namey,1, 6) # get the first 6 characters containing the date
H$selection<- word(namey, -1) #get the last word
H$treatment<- word(namey, -2) # get the second last word
H$REP<- word(namey, -3) # get the third last word
return(H)
}


Then imap passes the data to the H argument and the element names to the namey argument.



ListofData.tagged <- imap(ListofData, tagging)




base R translation



ListofData <- lapply(setNames(temp,temp), read.csv)
ListofData.tagged <- Map(tagging, ListofData, names(ListofData))




Or if you don't care about naming the elements of ListofData you can do directly Map(tagging, ListofData, temp) (still keeping the new definition of tagging).






share|improve this answer


























  • thanks @rui-barradas

    – Moody_Mudskipper
    Nov 16 '18 at 18:57











  • It works!!! Thank you so so so much!

    – user163731
    Nov 17 '18 at 14:08














0












0








0







Import this way so you keep your names:



library(tidyverse)
ListofData <- map(set_names(temp), read_csv)


then we modify your function to add the name as a second parameter, which will be used through imap, so we get rid of the first line too :



tagging <- function(H, namey){
namey<- str_sub(namey,1, -5) #drop the .csv
H$date<-substring(namey,1, 6) # get the first 6 characters containing the date
H$selection<- word(namey, -1) #get the last word
H$treatment<- word(namey, -2) # get the second last word
H$REP<- word(namey, -3) # get the third last word
return(H)
}


Then imap passes the data to the H argument and the element names to the namey argument.



ListofData.tagged <- imap(ListofData, tagging)




base R translation



ListofData <- lapply(setNames(temp,temp), read.csv)
ListofData.tagged <- Map(tagging, ListofData, names(ListofData))




Or if you don't care about naming the elements of ListofData you can do directly Map(tagging, ListofData, temp) (still keeping the new definition of tagging).






share|improve this answer















Import this way so you keep your names:



library(tidyverse)
ListofData <- map(set_names(temp), read_csv)


then we modify your function to add the name as a second parameter, which will be used through imap, so we get rid of the first line too :



tagging <- function(H, namey){
namey<- str_sub(namey,1, -5) #drop the .csv
H$date<-substring(namey,1, 6) # get the first 6 characters containing the date
H$selection<- word(namey, -1) #get the last word
H$treatment<- word(namey, -2) # get the second last word
H$REP<- word(namey, -3) # get the third last word
return(H)
}


Then imap passes the data to the H argument and the element names to the namey argument.



ListofData.tagged <- imap(ListofData, tagging)




base R translation



ListofData <- lapply(setNames(temp,temp), read.csv)
ListofData.tagged <- Map(tagging, ListofData, names(ListofData))




Or if you don't care about naming the elements of ListofData you can do directly Map(tagging, ListofData, temp) (still keeping the new definition of tagging).







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 18:55









Rui Barradas

18.3k51833




18.3k51833










answered Nov 16 '18 at 18:47









Moody_MudskipperMoody_Mudskipper

25k33673




25k33673













  • thanks @rui-barradas

    – Moody_Mudskipper
    Nov 16 '18 at 18:57











  • It works!!! Thank you so so so much!

    – user163731
    Nov 17 '18 at 14:08



















  • thanks @rui-barradas

    – Moody_Mudskipper
    Nov 16 '18 at 18:57











  • It works!!! Thank you so so so much!

    – user163731
    Nov 17 '18 at 14:08

















thanks @rui-barradas

– Moody_Mudskipper
Nov 16 '18 at 18:57





thanks @rui-barradas

– Moody_Mudskipper
Nov 16 '18 at 18:57













It works!!! Thank you so so so much!

– user163731
Nov 17 '18 at 14:08





It works!!! Thank you so so so much!

– user163731
Nov 17 '18 at 14:08




















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343205%2ffunction-works-on-one-element-of-list-doesnt-work-on-full-list-r%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







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values