Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
140 views
in Technique[技术] by (71.8m points)

Is hlookup possible in R? (or, how do I conditionally populate a column?)

I have two dataframes.

X1 is

size    count
37      1.181
38      0.421
39      0.054

and X2 is

size    v1    v1
37      1     5
...     ...   ...
37      1     5
38      3     3
...     ...   ...
38      3     3
39      5     6
...     ...   ...
39      5     6

Dataframe X2 is a large panel basically. I want to create a 3rd column, v3, in X2, such that it takes the value of count from X1 if size matches.

How is this possible?

question from:https://stackoverflow.com/questions/65941178/is-hlookup-possible-in-r-or-how-do-i-conditionally-populate-a-column

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can join the two datasets together like this:

library(dplyr)
left_join(X2, X1, by="size")

Example with the Iris dataset:

I2<-data.frame(Species=c("setosa", "versicolor",  "virginica"),
               myData=c(1,2,3))
left_join(iris, I2, by="Species")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...