0

I have 2 data.frames, one is a base (test_base ) in which I am working and the other is the changes to be made in the base (obs_changes).

Here are the codes of the 2 bases so you can look at it:

test_base<- tibble(id= 1:25,

name= as.factor(sample(c("Celeste", "Fernando", "Juan", "Lucia", "Maria", "Pablo"), 25, replace= T)),

class= as.factor(sample(c("A", "B", "C", "D"), 25, replace= T)),

note= rnorm(25, mean= 6, sd=1),

country= as.factor(sample(c("Argentina", "France", "USA"), 25, replace= T)))

head(test_base)

id name class note country

1 1 Fernando D 6.63 Argentina

2 2 Pablo D 6.79 Argentina

3 3 Juan B 4.61 Argentina

4 4 Celeste B 6.30 France

5 5 Celeste D 7.25 France

6 6 Celeste D 7.27 Argentina


obs_changes

structure(list(date = structure(1647820800, tzone = "UTC", class = c("POSIXct",

"POSIXt")), base_archive = "test_base ", n_rows = 25, n_cols = 5,

id = 5, variable = "name", previous_value = "Fernando", new_value = "Lucas"), row.names = c(NA, -1L), class = c("tbl_df",

"tbl", "data.frame")) 


view(obs_changes)

date   base_archive   n_rows   n_cols id variable previous_value new_value

<dttm> <chr> <dbl> <dbl> <dbl> <chr> <chr> <chr>

2022-03-21 "test_base " 25      5      5   name     Fernando        Lucas

obs_changes$variable is the name of the column of base_test that I need to modify.

> obs_changes$variable

[1] "name"

base_test has a column named "name" and i need to modify one of the values

I need to make changes to the base but following the values ​​that appear in obs_changes. The problem appears when I want to apply mutate() on the base and it doesn't let me, I don't understand why.

For example, when I do

test_base %>%

mutate(obs_changes$variable= 2)

throws me an error

Error: unexpected '=' in: " test_base %>%

mutate(obs_changes$variable="

But the strange thing is that in other syntaxes there is no problem, for example:

base_test[, obs_changes$variable]

brings me without problems the column

I just want to make the change at the row where id == 5. Ideally, if the mutate worked for me, I would do this:

test_base %>%
    mutate(obs_changes$variable = if_else(id == obs_changes$id & obs_changes$variable == obs_changes$previous_value,
                                          obs_changes$new_value,
                                          obs_changes$variable))
juandmaz
  • 43
  • 5

0 Answers0