Change the value of a variable if it meets a condition in R

2

I need to change the value of a variable.

One of them is called Village and one of them is called site . One of the Villages that is called esperanza_paleta has value 5 in variable site and I want that 5 change to 4. Bone when the Village: esperanza_paleta has value 5 in the site variable, change it to 4.

Try, but I get it as follows:

> data <- data[ which(data$country=="peru"), ]#
> summary(data$site)
  2   3   4   5   6 
  0   0 833   2 171 
> data$site<- ifelse(data$village=="esperanza_paleta" & data$site==5,4,data$site)
> data$site<- as.factor(data$site)
> summary(data$site)
  3   4   5
833   2 171 
    
asked by user57214 24.08.2017 в 18:53
source

3 answers

1

A fourth option using the dplyr package (for those who prefer the tidyverse ):

library(dplyr)

car_antes <- tibble(carro = c(2, 2, 12, 10, 24, 30, 50, 55, 60),
                    color = rep(c("red", "white", "yellow"), 3)) # vectores de mismo largo

car_despues <- car_antes %>% 
                 mutate(carro = ifelse(color == "red", 1, carro))

Which would give you for car_antes :

# A tibble: 9 × 2
  carro  color
  <dbl>  <chr>
1     2    red
2     2  white
3    12 yellow
4    10    red
5    24  white
6    30 yellow
7    50    red
8    55  white
9    60 yellow

And for car_despues :

# A tibble: 9 × 2
  carro  color
  <dbl>  <chr>
1     1    red
2     2  white
3    12 yellow
4     1    red
5    24  white
6    30 yellow
7     1    red
8    55  white
9    60 yellow

If you need to modify the color variable several times according to different conditions, the case_when() function (also% of dplyr ) may be more useful to you.

    
answered by 29.08.2017 в 23:04
0

I do not know if I am interpreting you properly, but I understand that in your example you want to modify the value of the column / variable carro by 1 if color is red . The simplest way, I think it is this:

d <- c(2,2,12,10, 24,30,50,55,60)
e <- c("red", "white", "yellow")
car <- data.frame(d,e)
names(car) <- c("carro","Color") # variable names

car
car$carro[car$Color == "red"] <- 1
car

The before:

  carro  Color
1     2    red
2     2  white
3    12 yellow
4    10    red
5    24  white
6    30 yellow
7    50    red
8    55  white
9    60 yellow

The after:

  carro  Color
1     1    red
2     2  white
3    12 yellow
4     1    red
5    24  white
6    30 yellow
7     1    red
8    55  white
9    60 yellow
    
answered by 24.08.2017 в 19:11
0

The solution that you have provided is the correct one and you should always go to the easiest solution, I bring you other approaches only with a didactic purpose that also achieve the same:

d <- c(2,2,12,10, 24,30,50,55,60)
e <- c("red", "white", "yellow")
car <- data.frame(d,e)
names(car) <- c("carro","Color") # variable names

# con plyr, muy fácil
library(plyr)
car$Color <- revalue(car$Color, c("red"=1))

# opcion2, cuando son factores
levels(car$Color)[levels(car$Color)=="red"] <- 1

# opcion 3, within
car <- within(car, levels(Color)[levels(Color) == "red"] <- 1)
    
answered by 28.08.2017 в 09:57