0

I want to add new column Petal which is product of two other columns to the data frame and it works with below code

b %>% 
mutate(Petal = Petal.Length * Petal.Width) %>%
head 

but the problem is when I print b after, Petal diapears.

I tried also this but it brings error

b <- mutate(Petal = Petal.Length*Petal.Width)

so, how can I save it?

zx8754
  • 52,746
  • 12
  • 114
  • 209
Garegin
  • 61
  • 1
  • 9
  • 1
    You need to assign it to a new object or the old object i.e. `b <- b %>% mutate(Petal = Petal.Length * Petal.Width)` or use the `%<>%` operator from `magrittr` i.e. `library(magrittr); b %<>% mutate(Petal = Petal.Length * Petal.Width)` – akrun Feb 12 '17 at 17:33
  • 1
    both versions that you suggested work, thank you. – Garegin Feb 12 '17 at 17:40
  • I don't know if i need to open a new question or not but my question is that I have 150 rows but after I run mutate command only 6 are left, what is the problem? – Garegin Feb 12 '17 at 17:44
  • Please check the `str(b)` It must be a printing problem on the console by the `tbl_df` object . Try `b %>% as.data.frame()` – akrun Feb 12 '17 at 17:45
  • b is a data.frame – Garegin Feb 12 '17 at 17:53
  • `head` prints 6 rows by default. – Axeman Feb 12 '17 at 18:46

0 Answers0