1

I'm very new to R. I want to rename the column (total=n()) or create a new one from that one. Can I do it simply in R or should I export the dataframe into Excel, and build the column in Excel.

   State      Year       Weapon_label     (total = n())
   Alaska      1976      Other weapons            1
   Alabama     1976      Other weapons           16
   Arkansas    1976      Other weapons            5
   Arizona     1976      Other weapons            6

To get (total=n()) I code:

murderso <- murderso %>% 
  select(State, Year, Weapon_label) %>% 
  group_by(State, Year, Weapon_label) %>% 
  summarise(total=n())

Any suggestion for a beginner? Thanks for help

  • 4
    There is `dplyr::rename()`. But I would have thought the code in your example would generate a column named `total`. Or is `(total = n())` the name that you want? – neilfws Nov 27 '18 at 22:48
  • 2
    Do you mean `... %>% summarise("(total = n())"=n())`? – r2evans Nov 27 '18 at 22:49
  • @neilfws Unfortunately, not. Here the snapshot. – Jeane Vivant Nov 27 '18 at 22:52
  • Unfortunately, not. I get (total = n()). I want to rename it "Total1" because I want to join this dataframe to another one , then calculate the percentage of (total = n()).x of (total = n()).y from the second dataframe. I can't do it with this spelling. – Jeane Vivant Nov 27 '18 at 22:59
  • 1
    Something is going wrong that we can't see, your code should give a variable called `total`! – Axeman Nov 27 '18 at 23:00
  • 1
    Can you please try this using the iris data set and include the code. – Elin Nov 27 '18 at 23:01
  • @neilfws I tried dplyr::rename.murderso<- rename(murderso$`(total = n())`, "Total1") with '(total=n())' but error message " Error in UseMethod("rename_") : no applicable method for 'rename_' applied to an object of class "c('integer', 'numeric')" – Jeane Vivant Nov 27 '18 at 23:03
  • I'm going to guess this could be a "restart R and run it again" type of problem – camille Nov 27 '18 at 23:06
  • @camille I did it several times. No success. Definitively the way of calling (total=n()) – Jeane Vivant Nov 27 '18 at 23:08
  • JeaneVivant, your code does not produce that in others' consoles, so there is something else going on here. Try camille's recommendation to restart R and try again ... if it repeats, then you will need to provide a reproducible question. By this I mean using data we have or give us just-enough-data so we can reproduce it (e.g., `dput(head(murderso))`), and (if not the same) the verbatim code used that generates the above. Refs: https://stackoverflow.com/questions/5963269, https://stackoverflow.com/help/mcve, and https://stackoverflow.com/tags/r/info. – r2evans Nov 27 '18 at 23:08
  • `chickwts %>% dplyr::group_by(feed) %>% dplyr::summarise(total = n())` works fine for me, try adding the dplyr:: . – Elin Nov 27 '18 at 23:08
  • @Elin, good thought, I was just checking similar. There are similar functions in packages `Hmisc` and `plyr`, though neither produces this behavior. – r2evans Nov 27 '18 at 23:10
  • @r2evans thanks I'll try later (midnight, now, in my place) I spent 3 hours to try different ways. ;-)) – Jeane Vivant Nov 27 '18 at 23:10
  • 1
    Bottom line, though, without your data we cannot reproduce it so we cannot help troubleshoot it. If you can generate with `mtcars` or `iris` or some other common (even base-R) dataset, you'll get much more detailed responses. Good luck, Jeane. – r2evans Nov 27 '18 at 23:12
  • not sure how you got that name, but `names(murderso)[4] = 'total'` should fix it – dww Nov 28 '18 at 00:42

1 Answers1

0

You don't want to do anything in Excel, except maybe data entry: this would be tedious, but it also leaves no trace. You want to treat your data as read-only and do all transformations in R. That way, you keep track of what you did and you can change your mind without ever risking to loose the data or get confused as to what was the original state.

So don't export a data frame to excel to transform it and re-import it into R! Do it all in R.

library(tidyverse)  

Applying your code, only modifying the name of the variable created by summarise() to Total1 (here with the dataset iris as an example):

iris %>% 
      select(Species, Sepal.Length, Sepal.Width) %>% 
      group_by(Species, Sepal.Length, Sepal.Width) %>% 
      summarise(Total1 = n())

Result:

# A tibble: 127 x 4
# Groups:   Species, Sepal.Length [?]
   Species Sepal.Length Sepal.Width Total1
   <fct>          <dbl>       <dbl>  <int>
 1 setosa           4.3         3        1
 2 setosa           4.4         2.9      1
 3 setosa           4.4         3        1
 4 setosa           4.4         3.2      1
 5 setosa           4.5         2.3      1
 6 setosa           4.6         3.1      1
 7 setosa           4.6         3.2      1
 8 setosa           4.6         3.4      1
 9 setosa           4.6         3.6      1
10 setosa           4.7         3.2      2
# ... with 117 more rows
prosoitos
  • 6,679
  • 5
  • 27
  • 41
  • many thanks for this shortcut to get the right name for the column but as a previous user suggested : I restarted R. And it worked! Not used yet with these problems – Jeane Vivant Nov 28 '18 at 10:31
  • Glad you got your problem fixed. And yes: restarting R (and not reloading any environment (i.e. `.Rda` or `.Rdata` file)) is always a good practice. – prosoitos Nov 28 '18 at 16:33