dimanche 28 mars 2021

How to change values of a column according to some conditions?

I have a data frame named data1.
> data1 <- data.frame(name = c("apple","apple","apple", "pine","pine","pine", 
                                 "apple","apple","apple", "pine","pine","pine"),
                        characters = c("red","green","white","yellow","brown","black",
                                       "big","sweet","small","delicious","medium","ache"))
> data1  
    name characters
1  apple        red
2  apple      green
3  apple      white
4   pine     yellow
5   pine      brown
6   pine      black
7  apple        big
8  apple      sweet
9  apple      small
10  pine  delicious
11  pine     medium
12  pine       ache

And I want to change the values of the name variable according the values of characters column.Just like data2:

> data2
        name characters
1   colapple        red
2   colapple      green
3   colapple      white
4  colorpine     yellow
5  colorpine      brown
6  colorpine      black
7   othapple        big
8   othapple      sweet
9   othapple      small
10   despine  delicious
11   despine     medium
12   despine       ache

How can I realize it?

Aucun commentaire:

Enregistrer un commentaire