Jeremy bravo
Jeremy bravo

Reputation: 5

How do we replace a column in a tibble with another vector of identical length without changing the variable name?

I have a large data frame that I coerced as a tibble to be able to use the dplyr package. I wanted to know if there was a way to "replace" a column in the tibble with the same variable in a different notation.

I have tried the mutate() function but I don't want to add a new column to the tibble, just replace a column with a vector of the same length.

Upvotes: 0

Views: 551

Answers (2)

Giovana Stein
Giovana Stein

Reputation: 459

You just need to set the name of your variable inside the mutate. For example, if you want to divide by 100:

mutate(var = var/100)

Upvotes: 1

Ben Bolker
Ben Bolker

Reputation: 226192

If I understand your question correctly, I think the answer is mutate()!

> library(dplyr)
> d <- tibble(x=1:3,y=2:4)
> d <- d %>% mutate(x=8:10) ## replace column x
> d
# A tibble: 3 x 2
      x     y
  <int> <int>
1     8     2
2     9     3
3    10     4

Upvotes: 1

Related Questions