How to replace the tag value with the corresponding actual value in R?

Hello, everyone . I have too different dataframes. Example1 is number's id and corresponding actual value that columnname is time. Example2 is a dataframe that have many id number. How to replace all the id number in Example2 with the corresponding actual value as it is shown in Example1 in R? Thank you in advance. I am new users. The community doesn't allow to send .csv files or others. if you need, would you mind write your email address and i send them to you.

Here is an example of what I think you want to do. I appended the values rather than directly substituting the value for the id. Notice that I invented some data. That is often a better way to make an example than uploading a data file.

Example1 <- data.frame(id=c(1,2,3,4,5),
                       Value=c(23,31,1,6,10))
Example1
#>   id Value
#> 1  1    23
#> 2  2    31
#> 3  3     1
#> 4  4     6
#> 5  5    10

Example2 <- data.frame(id=c(4,5,3,1,2,4))
Example2
#>   id
#> 1  4
#> 2  5
#> 3  3
#> 4  1
#> 5  2
#> 6  4
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
Example2 <- inner_join(Example2,Example1,by="id")
Example2
#>   id Value
#> 1  4     6
#> 2  5    10
#> 3  3     1
#> 4  1    23
#> 5  2    31
#> 6  4     6

#If you want to drop the id column
Example2$id <- NULL
Example2
#>   Value
#> 1     6
#> 2    10
#> 3     1
#> 4    23
#> 5    31
#> 6     6

Created on 2022-10-28 with reprex v2.0.2

It sounds like you are looking for a "join" operation, take a look at the dplyr package.

That is because the preferred method for asking questions here is with a proper Reproducible Example, take a look at this guide to learn how to create one.

I am grateful to your effort in this matter. One more question, if in Example2, there are many column of id numbers. And their colnumames are not"id". How could we replace them with actual value?

Example2
#> id1 id2 id3
#> 1 4 5 3
#> 2 5 4 1
#> 3 3 2 2
#> 4 1 1 4
#> 5 2 3 5
#> 6 4 4 NA
屏幕截图 2022-10-29 112019

My example data code

id<-c(1,2,3,4,5,6,7,8,9,10)
time<-c(6.2,3.8,2.9,6.7,9.2,4.6,8.8,1.7,2.3,3.6)
Example1<-data.frame(id,time)

c1<-c(3,6,4,5,8,10,NA,1)
c2<-c(6,7,3,1,6,4,9,2)
c3<-c(5,6,7,1,2,7,4,2)
Example2<-data.frame(c1,c2,c3)

Since there are multiple columns to change, I would use a different method than in my first post.

Example1 <- data.frame(id=c(1,2,3,4,5),
                       Value=c(23,31,1,6,10))
Example1
#>   id Value
#> 1  1    23
#> 2  2    31
#> 3  3     1
#> 4  4     6
#> 5  5    10

Example2 <- data.frame(c1 = sample(1:5, 6, replace = TRUE),
                       c2 = sample(1:5, 6, replace = TRUE),
                       c3 = sample(1:5, 6, replace = TRUE),
                       c4 = sample(1:5, 6, replace = TRUE))
                       
Example2
#>   c1 c2 c3 c4
#> 1  1  2  3  3
#> 2  2  2  3  5
#> 3  4  2  2  4
#> 4  4  4  4  5
#> 5  3  5  5  2
#> 6  1  3  2  4
Lookup <- Example1$Value
names(Lookup) <- Example1$id
Lookup
#>  1  2  3  4  5 
#> 23 31  1  6 10
library(dplyr)

Example2 <- Example2 |> mutate(across(.cols = everything(),.fns = ~Lookup[.x]))
Example2
#>   c1 c2 c3 c4
#> 1 23 31  1  1
#> 2 31 31  1 10
#> 3  6 31 31  6
#> 4  6  6  6 10
#> 5  1 10 10 31
#> 6 23  1 31  6

Created on 2022-10-28 with reprex v2.0.2

I am grateful to you. I just make it. Knowing how to asking peoblems properly.

Generally, I am appreciative of your effort in this matter. What's more, if there are some not available data(NA) in Example2, does it have a influence? I just learn R recently.

If there is an NA in Example2, it will remain NA.

I am sorry to disturb you agagin. i just don't know the meaning of this symbol"|>" in the code. matching?

These are very different questions, we like to keep things tidy around here so just one reasonably well defined question per topic and closely related follow ups. Can you please ask these on new topicd providing a relevant REPRoducible EXample (reprex) for your new issue?

OK, I am very sorry. I will obey this rule more carefully. Because I am new user, platform tell me i could only add new topic after 10 more hours. Just now, I delete the question that is different from the original topic.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.