如何在不同的数据框中找到对应的列

huangapple go评论66阅读模式
英文:

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 &lt;- data.frame(
hi_ras1_55 = c(1:5), 
kk.ras2_ml= c(6:10),
ii_ras4.kj = c(11:15)) 

 pp &lt;- data.frame(
emp_id = c (1:5), 
emp_name = c(&quot;Rick&quot;,&quot;Dan&quot;,&quot;Michelle&quot;,&quot;Ryan&quot;,&quot;Gary&quot;),
salary = c(&quot;ras1&quot;,&quot;ras2&quot;,&quot;ras3&quot;,&quot;ras3&quot;,&quot;ras4&quot;)) 

dat &lt;- 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[&quot;emp_name&quot;] == names(dat)[2], &quot;salary&quot;]

Now use it to add the column that includes this string to dat using cbind:

dat &lt;- 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)

如何在不同的数据框中找到对应的列
extractor(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&lt;-function(dat, pp, emp){
 # get the employee name save it to variable
  gent=colnames(dat)[2]

 #use variable to select the salary class
 salary&lt;-pp%&gt;%
    filter(emp_name==agent)%&gt;%
    select(salary)%&gt;%
    pull()
#create output values from the emp dataframe
  output&lt;-emp%&gt;%
    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&lt;-cbind(dat, output)
  return(output)
}
extrator(dat,pp, emp)

如何在不同的数据框中找到对应的列
extractor(dat,pp, emp)

huangapple
  • 本文由 发表于 2023年6月15日 02:37:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476638.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定