英文:
How to find corresponding column in differnt data frames
问题
# Read the 'dat' data frame
dat <- data.frame(
gg = c(21:25),
Rick = c(6:10)
)
# Extract the name of the second column in 'dat'
second_column_name <- names(dat)[2]
# Extract the corresponding value under the 'salary' column in 'pp'
corresponding_salary <- pp[pp$emp_name == second_column_name, "salary"]
# Find the column in 'emp' that contains the name from 'corresponding_salary'
matching_column <- emp[, grepl(corresponding_salary, names(emp))]
# Rename the matching column in 'dat' to "final"
names(dat)[2] <- "final"
# Combine 'dat' with the matching column from 'emp'
result <- cbind(dat, matching_column)
# Print the desired output
print(result)
Output:
gg final
1 21 1
2 22 2
3 23 3
4 24 4
5 25 5
英文:
emp <- data.frame(
hi_ras1_55 = c(1:5),
kk.ras2_ml= c(6:10),
ii_ras4.kj = c(11:15))
pp <- data.frame(
emp_id = c (1:5),
emp_name = c("Rick","Dan","Michelle","Ryan","Gary"),
salary = c("ras1","ras2","ras3","ras3","ras4"))
dat <- data.frame(
gg = c(21:25),
Rick= c(6:10))
I want to read dat and then extract the name of the second column "Rick" and then extract what corresponds to it under the column salary in pp "ras1" and then find the column in emp that contains this name "hi_ras1_55" and put the column in dat with a title "final".
desired output:
gg Rick final
21 6 1
22 7 2
23 8 3
24 9 4
25 10 5
答案1
得分: 1
首先,使用列名找到salary
条目:
salary = pp[pp["emp_name"] == names(dat)[2], "salary"]
现在,使用它来添加包含此字符串的列到dat
,使用cbind
:
dat <- cbind(dat, final = emp[, grepl(salary, names(emp))])
函数grepl
被用来测试列名是否包含salary
。
英文:
First find the salary
entry using the column name:
salary = pp[pp["emp_name"] == names(dat)[2], "salary"]
Now use it to add the column that includes this string to dat
using cbind
:
dat <- cbind(dat, final = emp[, grepl(salary, names(emp))])
Function grepl
was used to test whether column name contains salary
.
答案2
得分: 1
I am a create a function person if you need to consistently do this over and over, which it seems like one might want to do from the sample:
extractor<-function(dat, pp, emp){
# get the employee name save it to variable
gent=colnames(dat)[2]
#use variable to select the salary class
salary<-pp %>%
filter(emp_name==agent) %>%
select(salary) %>%
pull()
#create output values from the emp dataframe
output<-emp %>%
select(contains(salary))
#bind the output to the original data this can be piped from above
#but the order of the variables will not be as you indicated
output<-cbind(dat, output)
return(output)
}
extrator(dat,pp, emp)
英文:
I am a create a function person if you need to consistently do this over and over, which it seems like one might want to do from the sample:
extractor<-function(dat, pp, emp){
# get the employee name save it to variable
gent=colnames(dat)[2]
#use variable to select the salary class
salary<-pp%>%
filter(emp_name==agent)%>%
select(salary)%>%
pull()
#create output values from the emp dataframe
output<-emp%>%
select(contains(salary))
#bind the output to the original data this can be piped from above
#but the order of the variables will not be as you indicated
output<-cbind(dat, output)
return(output)
}
extrator(dat,pp, emp)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论