How to copy attributes from one data frame to another or to re-assign attributes to a freshly transposed data frame - RHow to join (merge) data frames (inner, outer, left, right)How to convert a data frame column to numeric type?How to drop columns by name in a data frameHow does one reorder columns in a data frame?Transpose a data frameExtracting specific columns from a data frameHow to convert a table to a data frameTransposing a data frameFind AUC with tree package - binary responseTransposing data frames

What is the correct parsing of お高くとまる?

Is there a nice way to implement a conditional type with default fail case?

How quality assurance engineers test calculations?

LED glows slightly during soldering

What exactly is the relationship between Flow/Feed Rate and (Print) Speed?

When did "&" stop being taught alongside the alphabet?

Is there a method for differentiating informative comments from commented out code?

Is it possible to see individual photons impressioning film?

What's it called when the bad guy gets eaten?

How many tone holes are there actually in different orchestral woodwind instruments?

Why is a mixture of two normally distributed variables only bimodal if their means differ by at least two times the common standard deviation?

How could a turbocharger experience over-boosting due to cold oil?

Is there any reason why MCU changed the Snap to Blip

What is a "Lear Processor" and how did it work?

What adjective means "accurately representitive of reality"?

Why did Harry Potter get a bedroom?

Do I have to worry about delays in international trains? (UK, Belgium, Germany)

Why different specifications for telescopes and binoculars?

What are some further readings in Econometrics you recommend?

What is the parallel of Day of the Dead with Stranger things?

The three greedy pirates

Is that a case of "DOUBLE-NEGATIVES" as claimed by Grammarly?

What is the minimum time required for final wash in film development?

What could cause the sea level to massively decrease?



How to copy attributes from one data frame to another or to re-assign attributes to a freshly transposed data frame - R


How to join (merge) data frames (inner, outer, left, right)How to convert a data frame column to numeric type?How to drop columns by name in a data frameHow does one reorder columns in a data frame?Transpose a data frameExtracting specific columns from a data frameHow to convert a table to a data frameTransposing a data frameFind AUC with tree package - binary responseTransposing data frames






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















After transposing data I'd like to re-assign attributes that are dropped. This could also be applicable to copying attributes from one data frame to another. Or copying attributes after mutates, etc., where they are dropped.



 library(reshape2)

df <- data.frame(id = c(1,2,3,4,5),
time = c(11, 22,33,44,55),
c = c(1,2,3,5,5),
d = c(4,2,5,4,NA))

attr(df$id,"label")<- "label"
attr(df$time,"label")<- "label2"
attr(df$c,"label")<- "something here"
attr(df$d,"label")<- "count of something"
str(df)

str(df)
data.frame': 5 obs. of 4 variables:
$ id : num 1 2 3 4 5
..- attr(*, "label")= chr "label"
$ time: num 11 22 33 44 55
..- attr(*, "label")= chr "label2"
$ c : num 1 2 3 5 5
..- attr(*, "label")= chr "something here"
$ d : num 4 2 5 4 NA
..- attr(*, "label")= chr "count of something"


Cast to wide



dfwide<- recast(df,id~variable +time, 
id.var = c("id","time"))


Usual attribute lost message:



 Warning message:
attributes are not identical across measure variables; they will be dropped

str(dfwide)
'data.frame': 5 obs. of 11 variables:
$ id : num 1 2 3 4 5
$ c_11: num 1 NA NA NA NA
$ c_22: num NA 2 NA NA NA
$ c_33: num NA NA 3 NA NA
$ c_44: num NA NA NA 5 NA
$ c_55: num NA NA NA NA 5
$ d_11: num 4 NA NA NA NA
$ d_22: num NA 2 NA NA NA
$ d_33: num NA NA 5 NA NA
$ d_44: num NA NA NA 4 NA
$ d_55: num NA NA NA NA NA


Using mostattributes one can copy attributes between dataframes, but for iterations over many column names I can't figure out or think about how to map this efficiently in a different way save one by one.



 mostattributes(dfwide$c_11)<-attributes(df$c)
mostattributes(dfwide$c_22)<-attributes(df$c)
> str(dfwide)
'data.frame': 5 obs. of 11 variables:
$ id : num 1 2 3 4 5
$ c_11: num 1 NA NA NA NA
..- attr(*, "label")= chr "something here"
$ c_22: num NA 2 NA NA NA
..- attr(*, "label")= chr "something here"
$ c_33: num NA NA 3 NA NA


I was trying to automate it but failed (all c's should have same labels and d's have same labels):



#extract arguments
dlist<-enframe(names(df))%>%
slice(-1,-2)%>%
pull(., value)
dlist

dlistw<-enframe(names(dfwide))%>%
slice(-1)%>%
pull(., value)
dlistw

#function
mostatt<- function(var1, var2)
mostattributes(dfwide[[var1]])<<-attributes(df[[var2]])


mapply(mostatt,dlistw,dlist)
str(dfwide)

'data.frame': 5 obs. of 11 variables:
$ id : num 1 2 3 4 5
$ c_11: num 1 NA NA NA NA
..- attr(*, "label")= chr "something here"
$ c_22: num NA 2 NA NA NA
..- attr(*, "label")= chr "count of something"
$ c_33: num NA NA 3 NA NA
..- attr(*, "label")= chr "something here"
$ c_44: num NA NA NA 5 NA
..- attr(*, "label")= chr "count of something"
$ c_55: num NA NA NA NA 5
..- attr(*, "label")= chr "something here"
$ d_11: num 4 NA NA NA NA
..- attr(*, "label")= chr "count of something"
$ d_22: num NA 2 NA NA NA
..- attr(*, "label")= chr "something here"
$ d_33: num NA NA 5 NA NA
..- attr(*, "label")= chr "count of something"
$ d_44: num NA NA NA 4 NA
..- attr(*, "label")= chr "something here"
$ d_55: num NA NA NA NA NA
..- attr(*, "label")= chr "count of something"


I think using tidyselect starts_with might be worth a try but not sure how to incorporate it. Any suggestions would be appreciated. Thank you!










share|improve this question






























    1















    After transposing data I'd like to re-assign attributes that are dropped. This could also be applicable to copying attributes from one data frame to another. Or copying attributes after mutates, etc., where they are dropped.



     library(reshape2)

    df <- data.frame(id = c(1,2,3,4,5),
    time = c(11, 22,33,44,55),
    c = c(1,2,3,5,5),
    d = c(4,2,5,4,NA))

    attr(df$id,"label")<- "label"
    attr(df$time,"label")<- "label2"
    attr(df$c,"label")<- "something here"
    attr(df$d,"label")<- "count of something"
    str(df)

    str(df)
    data.frame': 5 obs. of 4 variables:
    $ id : num 1 2 3 4 5
    ..- attr(*, "label")= chr "label"
    $ time: num 11 22 33 44 55
    ..- attr(*, "label")= chr "label2"
    $ c : num 1 2 3 5 5
    ..- attr(*, "label")= chr "something here"
    $ d : num 4 2 5 4 NA
    ..- attr(*, "label")= chr "count of something"


    Cast to wide



    dfwide<- recast(df,id~variable +time, 
    id.var = c("id","time"))


    Usual attribute lost message:



     Warning message:
    attributes are not identical across measure variables; they will be dropped

    str(dfwide)
    'data.frame': 5 obs. of 11 variables:
    $ id : num 1 2 3 4 5
    $ c_11: num 1 NA NA NA NA
    $ c_22: num NA 2 NA NA NA
    $ c_33: num NA NA 3 NA NA
    $ c_44: num NA NA NA 5 NA
    $ c_55: num NA NA NA NA 5
    $ d_11: num 4 NA NA NA NA
    $ d_22: num NA 2 NA NA NA
    $ d_33: num NA NA 5 NA NA
    $ d_44: num NA NA NA 4 NA
    $ d_55: num NA NA NA NA NA


    Using mostattributes one can copy attributes between dataframes, but for iterations over many column names I can't figure out or think about how to map this efficiently in a different way save one by one.



     mostattributes(dfwide$c_11)<-attributes(df$c)
    mostattributes(dfwide$c_22)<-attributes(df$c)
    > str(dfwide)
    'data.frame': 5 obs. of 11 variables:
    $ id : num 1 2 3 4 5
    $ c_11: num 1 NA NA NA NA
    ..- attr(*, "label")= chr "something here"
    $ c_22: num NA 2 NA NA NA
    ..- attr(*, "label")= chr "something here"
    $ c_33: num NA NA 3 NA NA


    I was trying to automate it but failed (all c's should have same labels and d's have same labels):



    #extract arguments
    dlist<-enframe(names(df))%>%
    slice(-1,-2)%>%
    pull(., value)
    dlist

    dlistw<-enframe(names(dfwide))%>%
    slice(-1)%>%
    pull(., value)
    dlistw

    #function
    mostatt<- function(var1, var2)
    mostattributes(dfwide[[var1]])<<-attributes(df[[var2]])


    mapply(mostatt,dlistw,dlist)
    str(dfwide)

    'data.frame': 5 obs. of 11 variables:
    $ id : num 1 2 3 4 5
    $ c_11: num 1 NA NA NA NA
    ..- attr(*, "label")= chr "something here"
    $ c_22: num NA 2 NA NA NA
    ..- attr(*, "label")= chr "count of something"
    $ c_33: num NA NA 3 NA NA
    ..- attr(*, "label")= chr "something here"
    $ c_44: num NA NA NA 5 NA
    ..- attr(*, "label")= chr "count of something"
    $ c_55: num NA NA NA NA 5
    ..- attr(*, "label")= chr "something here"
    $ d_11: num 4 NA NA NA NA
    ..- attr(*, "label")= chr "count of something"
    $ d_22: num NA 2 NA NA NA
    ..- attr(*, "label")= chr "something here"
    $ d_33: num NA NA 5 NA NA
    ..- attr(*, "label")= chr "count of something"
    $ d_44: num NA NA NA 4 NA
    ..- attr(*, "label")= chr "something here"
    $ d_55: num NA NA NA NA NA
    ..- attr(*, "label")= chr "count of something"


    I think using tidyselect starts_with might be worth a try but not sure how to incorporate it. Any suggestions would be appreciated. Thank you!










    share|improve this question


























      1












      1








      1


      2






      After transposing data I'd like to re-assign attributes that are dropped. This could also be applicable to copying attributes from one data frame to another. Or copying attributes after mutates, etc., where they are dropped.



       library(reshape2)

      df <- data.frame(id = c(1,2,3,4,5),
      time = c(11, 22,33,44,55),
      c = c(1,2,3,5,5),
      d = c(4,2,5,4,NA))

      attr(df$id,"label")<- "label"
      attr(df$time,"label")<- "label2"
      attr(df$c,"label")<- "something here"
      attr(df$d,"label")<- "count of something"
      str(df)

      str(df)
      data.frame': 5 obs. of 4 variables:
      $ id : num 1 2 3 4 5
      ..- attr(*, "label")= chr "label"
      $ time: num 11 22 33 44 55
      ..- attr(*, "label")= chr "label2"
      $ c : num 1 2 3 5 5
      ..- attr(*, "label")= chr "something here"
      $ d : num 4 2 5 4 NA
      ..- attr(*, "label")= chr "count of something"


      Cast to wide



      dfwide<- recast(df,id~variable +time, 
      id.var = c("id","time"))


      Usual attribute lost message:



       Warning message:
      attributes are not identical across measure variables; they will be dropped

      str(dfwide)
      'data.frame': 5 obs. of 11 variables:
      $ id : num 1 2 3 4 5
      $ c_11: num 1 NA NA NA NA
      $ c_22: num NA 2 NA NA NA
      $ c_33: num NA NA 3 NA NA
      $ c_44: num NA NA NA 5 NA
      $ c_55: num NA NA NA NA 5
      $ d_11: num 4 NA NA NA NA
      $ d_22: num NA 2 NA NA NA
      $ d_33: num NA NA 5 NA NA
      $ d_44: num NA NA NA 4 NA
      $ d_55: num NA NA NA NA NA


      Using mostattributes one can copy attributes between dataframes, but for iterations over many column names I can't figure out or think about how to map this efficiently in a different way save one by one.



       mostattributes(dfwide$c_11)<-attributes(df$c)
      mostattributes(dfwide$c_22)<-attributes(df$c)
      > str(dfwide)
      'data.frame': 5 obs. of 11 variables:
      $ id : num 1 2 3 4 5
      $ c_11: num 1 NA NA NA NA
      ..- attr(*, "label")= chr "something here"
      $ c_22: num NA 2 NA NA NA
      ..- attr(*, "label")= chr "something here"
      $ c_33: num NA NA 3 NA NA


      I was trying to automate it but failed (all c's should have same labels and d's have same labels):



      #extract arguments
      dlist<-enframe(names(df))%>%
      slice(-1,-2)%>%
      pull(., value)
      dlist

      dlistw<-enframe(names(dfwide))%>%
      slice(-1)%>%
      pull(., value)
      dlistw

      #function
      mostatt<- function(var1, var2)
      mostattributes(dfwide[[var1]])<<-attributes(df[[var2]])


      mapply(mostatt,dlistw,dlist)
      str(dfwide)

      'data.frame': 5 obs. of 11 variables:
      $ id : num 1 2 3 4 5
      $ c_11: num 1 NA NA NA NA
      ..- attr(*, "label")= chr "something here"
      $ c_22: num NA 2 NA NA NA
      ..- attr(*, "label")= chr "count of something"
      $ c_33: num NA NA 3 NA NA
      ..- attr(*, "label")= chr "something here"
      $ c_44: num NA NA NA 5 NA
      ..- attr(*, "label")= chr "count of something"
      $ c_55: num NA NA NA NA 5
      ..- attr(*, "label")= chr "something here"
      $ d_11: num 4 NA NA NA NA
      ..- attr(*, "label")= chr "count of something"
      $ d_22: num NA 2 NA NA NA
      ..- attr(*, "label")= chr "something here"
      $ d_33: num NA NA 5 NA NA
      ..- attr(*, "label")= chr "count of something"
      $ d_44: num NA NA NA 4 NA
      ..- attr(*, "label")= chr "something here"
      $ d_55: num NA NA NA NA NA
      ..- attr(*, "label")= chr "count of something"


      I think using tidyselect starts_with might be worth a try but not sure how to incorporate it. Any suggestions would be appreciated. Thank you!










      share|improve this question
















      After transposing data I'd like to re-assign attributes that are dropped. This could also be applicable to copying attributes from one data frame to another. Or copying attributes after mutates, etc., where they are dropped.



       library(reshape2)

      df <- data.frame(id = c(1,2,3,4,5),
      time = c(11, 22,33,44,55),
      c = c(1,2,3,5,5),
      d = c(4,2,5,4,NA))

      attr(df$id,"label")<- "label"
      attr(df$time,"label")<- "label2"
      attr(df$c,"label")<- "something here"
      attr(df$d,"label")<- "count of something"
      str(df)

      str(df)
      data.frame': 5 obs. of 4 variables:
      $ id : num 1 2 3 4 5
      ..- attr(*, "label")= chr "label"
      $ time: num 11 22 33 44 55
      ..- attr(*, "label")= chr "label2"
      $ c : num 1 2 3 5 5
      ..- attr(*, "label")= chr "something here"
      $ d : num 4 2 5 4 NA
      ..- attr(*, "label")= chr "count of something"


      Cast to wide



      dfwide<- recast(df,id~variable +time, 
      id.var = c("id","time"))


      Usual attribute lost message:



       Warning message:
      attributes are not identical across measure variables; they will be dropped

      str(dfwide)
      'data.frame': 5 obs. of 11 variables:
      $ id : num 1 2 3 4 5
      $ c_11: num 1 NA NA NA NA
      $ c_22: num NA 2 NA NA NA
      $ c_33: num NA NA 3 NA NA
      $ c_44: num NA NA NA 5 NA
      $ c_55: num NA NA NA NA 5
      $ d_11: num 4 NA NA NA NA
      $ d_22: num NA 2 NA NA NA
      $ d_33: num NA NA 5 NA NA
      $ d_44: num NA NA NA 4 NA
      $ d_55: num NA NA NA NA NA


      Using mostattributes one can copy attributes between dataframes, but for iterations over many column names I can't figure out or think about how to map this efficiently in a different way save one by one.



       mostattributes(dfwide$c_11)<-attributes(df$c)
      mostattributes(dfwide$c_22)<-attributes(df$c)
      > str(dfwide)
      'data.frame': 5 obs. of 11 variables:
      $ id : num 1 2 3 4 5
      $ c_11: num 1 NA NA NA NA
      ..- attr(*, "label")= chr "something here"
      $ c_22: num NA 2 NA NA NA
      ..- attr(*, "label")= chr "something here"
      $ c_33: num NA NA 3 NA NA


      I was trying to automate it but failed (all c's should have same labels and d's have same labels):



      #extract arguments
      dlist<-enframe(names(df))%>%
      slice(-1,-2)%>%
      pull(., value)
      dlist

      dlistw<-enframe(names(dfwide))%>%
      slice(-1)%>%
      pull(., value)
      dlistw

      #function
      mostatt<- function(var1, var2)
      mostattributes(dfwide[[var1]])<<-attributes(df[[var2]])


      mapply(mostatt,dlistw,dlist)
      str(dfwide)

      'data.frame': 5 obs. of 11 variables:
      $ id : num 1 2 3 4 5
      $ c_11: num 1 NA NA NA NA
      ..- attr(*, "label")= chr "something here"
      $ c_22: num NA 2 NA NA NA
      ..- attr(*, "label")= chr "count of something"
      $ c_33: num NA NA 3 NA NA
      ..- attr(*, "label")= chr "something here"
      $ c_44: num NA NA NA 5 NA
      ..- attr(*, "label")= chr "count of something"
      $ c_55: num NA NA NA NA 5
      ..- attr(*, "label")= chr "something here"
      $ d_11: num 4 NA NA NA NA
      ..- attr(*, "label")= chr "count of something"
      $ d_22: num NA 2 NA NA NA
      ..- attr(*, "label")= chr "something here"
      $ d_33: num NA NA 5 NA NA
      ..- attr(*, "label")= chr "count of something"
      $ d_44: num NA NA NA 4 NA
      ..- attr(*, "label")= chr "something here"
      $ d_55: num NA NA NA NA NA
      ..- attr(*, "label")= chr "count of something"


      I think using tidyselect starts_with might be worth a try but not sure how to incorporate it. Any suggestions would be appreciated. Thank you!







      r attr purrr reshape2 tidyselect






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 26 at 17:53







      23stacks1254

















      asked Mar 25 at 20:30









      23stacks125423stacks1254

      747 bronze badges




      747 bronze badges






















          1 Answer
          1






          active

          oldest

          votes


















          1














          This is an option:



          for(i in (setdiff(colnames(df), "id")))
          for(x in colnames(dfwide)[(grepl(i, colnames(dfwide)))])
          mostattributes(dfwide[[x]]) <- attributes(df[[i]])

          mostattributes(dfwide$id) <- attributes(df$id)


          Because d is contained in id I need to rewrite id at the end.
          If you change d for e is even simplier:



          df <- data.frame(id = c(1,2,3,4,5), 
          time = c(11, 22,33,44,55),
          c = c(1,2,3,5,5),
          e = c(4,2,5,4,NA))


          attr(df$id,"label")<- "label"
          attr(df$time,"label")<- "label2"
          attr(df$c,"label")<- "something here"
          attr(df$e,"label")<- "count of something"
          str(df)

          dfwide<- recast(df,id~variable +time,
          id.var = c("id","time"))

          for(i in (colnames(df)))
          for(x in colnames(dfwide)[(grepl(i, colnames(dfwide)))])
          mostattributes(dfwide[[x]]) <- attributes(df[[i]])






          share|improve this answer


















          • 1





            That's a good option. Thank you for sharing. I didn't realize one could grep/grepl like that. I wonder if there is a way to add a regex carat ^ to the statement to signify the start position of the pattern. I'll play around with it. But as it is this solution works for me because the colnames in the dataframes I have are much longer and the grepl will work on those strings instead of a single character. Thank you again!

            – 23stacks1254
            Mar 26 at 17:41










          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%2f55345942%2fhow-to-copy-attributes-from-one-data-frame-to-another-or-to-re-assign-attributes%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









          1














          This is an option:



          for(i in (setdiff(colnames(df), "id")))
          for(x in colnames(dfwide)[(grepl(i, colnames(dfwide)))])
          mostattributes(dfwide[[x]]) <- attributes(df[[i]])

          mostattributes(dfwide$id) <- attributes(df$id)


          Because d is contained in id I need to rewrite id at the end.
          If you change d for e is even simplier:



          df <- data.frame(id = c(1,2,3,4,5), 
          time = c(11, 22,33,44,55),
          c = c(1,2,3,5,5),
          e = c(4,2,5,4,NA))


          attr(df$id,"label")<- "label"
          attr(df$time,"label")<- "label2"
          attr(df$c,"label")<- "something here"
          attr(df$e,"label")<- "count of something"
          str(df)

          dfwide<- recast(df,id~variable +time,
          id.var = c("id","time"))

          for(i in (colnames(df)))
          for(x in colnames(dfwide)[(grepl(i, colnames(dfwide)))])
          mostattributes(dfwide[[x]]) <- attributes(df[[i]])






          share|improve this answer


















          • 1





            That's a good option. Thank you for sharing. I didn't realize one could grep/grepl like that. I wonder if there is a way to add a regex carat ^ to the statement to signify the start position of the pattern. I'll play around with it. But as it is this solution works for me because the colnames in the dataframes I have are much longer and the grepl will work on those strings instead of a single character. Thank you again!

            – 23stacks1254
            Mar 26 at 17:41















          1














          This is an option:



          for(i in (setdiff(colnames(df), "id")))
          for(x in colnames(dfwide)[(grepl(i, colnames(dfwide)))])
          mostattributes(dfwide[[x]]) <- attributes(df[[i]])

          mostattributes(dfwide$id) <- attributes(df$id)


          Because d is contained in id I need to rewrite id at the end.
          If you change d for e is even simplier:



          df <- data.frame(id = c(1,2,3,4,5), 
          time = c(11, 22,33,44,55),
          c = c(1,2,3,5,5),
          e = c(4,2,5,4,NA))


          attr(df$id,"label")<- "label"
          attr(df$time,"label")<- "label2"
          attr(df$c,"label")<- "something here"
          attr(df$e,"label")<- "count of something"
          str(df)

          dfwide<- recast(df,id~variable +time,
          id.var = c("id","time"))

          for(i in (colnames(df)))
          for(x in colnames(dfwide)[(grepl(i, colnames(dfwide)))])
          mostattributes(dfwide[[x]]) <- attributes(df[[i]])






          share|improve this answer


















          • 1





            That's a good option. Thank you for sharing. I didn't realize one could grep/grepl like that. I wonder if there is a way to add a regex carat ^ to the statement to signify the start position of the pattern. I'll play around with it. But as it is this solution works for me because the colnames in the dataframes I have are much longer and the grepl will work on those strings instead of a single character. Thank you again!

            – 23stacks1254
            Mar 26 at 17:41













          1












          1








          1







          This is an option:



          for(i in (setdiff(colnames(df), "id")))
          for(x in colnames(dfwide)[(grepl(i, colnames(dfwide)))])
          mostattributes(dfwide[[x]]) <- attributes(df[[i]])

          mostattributes(dfwide$id) <- attributes(df$id)


          Because d is contained in id I need to rewrite id at the end.
          If you change d for e is even simplier:



          df <- data.frame(id = c(1,2,3,4,5), 
          time = c(11, 22,33,44,55),
          c = c(1,2,3,5,5),
          e = c(4,2,5,4,NA))


          attr(df$id,"label")<- "label"
          attr(df$time,"label")<- "label2"
          attr(df$c,"label")<- "something here"
          attr(df$e,"label")<- "count of something"
          str(df)

          dfwide<- recast(df,id~variable +time,
          id.var = c("id","time"))

          for(i in (colnames(df)))
          for(x in colnames(dfwide)[(grepl(i, colnames(dfwide)))])
          mostattributes(dfwide[[x]]) <- attributes(df[[i]])






          share|improve this answer













          This is an option:



          for(i in (setdiff(colnames(df), "id")))
          for(x in colnames(dfwide)[(grepl(i, colnames(dfwide)))])
          mostattributes(dfwide[[x]]) <- attributes(df[[i]])

          mostattributes(dfwide$id) <- attributes(df$id)


          Because d is contained in id I need to rewrite id at the end.
          If you change d for e is even simplier:



          df <- data.frame(id = c(1,2,3,4,5), 
          time = c(11, 22,33,44,55),
          c = c(1,2,3,5,5),
          e = c(4,2,5,4,NA))


          attr(df$id,"label")<- "label"
          attr(df$time,"label")<- "label2"
          attr(df$c,"label")<- "something here"
          attr(df$e,"label")<- "count of something"
          str(df)

          dfwide<- recast(df,id~variable +time,
          id.var = c("id","time"))

          for(i in (colnames(df)))
          for(x in colnames(dfwide)[(grepl(i, colnames(dfwide)))])
          mostattributes(dfwide[[x]]) <- attributes(df[[i]])







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 26 at 10:42









          LocoGrisLocoGris

          2,9332 gold badges10 silver badges28 bronze badges




          2,9332 gold badges10 silver badges28 bronze badges







          • 1





            That's a good option. Thank you for sharing. I didn't realize one could grep/grepl like that. I wonder if there is a way to add a regex carat ^ to the statement to signify the start position of the pattern. I'll play around with it. But as it is this solution works for me because the colnames in the dataframes I have are much longer and the grepl will work on those strings instead of a single character. Thank you again!

            – 23stacks1254
            Mar 26 at 17:41












          • 1





            That's a good option. Thank you for sharing. I didn't realize one could grep/grepl like that. I wonder if there is a way to add a regex carat ^ to the statement to signify the start position of the pattern. I'll play around with it. But as it is this solution works for me because the colnames in the dataframes I have are much longer and the grepl will work on those strings instead of a single character. Thank you again!

            – 23stacks1254
            Mar 26 at 17:41







          1




          1





          That's a good option. Thank you for sharing. I didn't realize one could grep/grepl like that. I wonder if there is a way to add a regex carat ^ to the statement to signify the start position of the pattern. I'll play around with it. But as it is this solution works for me because the colnames in the dataframes I have are much longer and the grepl will work on those strings instead of a single character. Thank you again!

          – 23stacks1254
          Mar 26 at 17:41





          That's a good option. Thank you for sharing. I didn't realize one could grep/grepl like that. I wonder if there is a way to add a regex carat ^ to the statement to signify the start position of the pattern. I'll play around with it. But as it is this solution works for me because the colnames in the dataframes I have are much longer and the grepl will work on those strings instead of a single character. Thank you again!

          – 23stacks1254
          Mar 26 at 17:41








          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















          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%2f55345942%2fhow-to-copy-attributes-from-one-data-frame-to-another-or-to-re-assign-attributes%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

          How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

          155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

          Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?