Assign Values Based on Variable Name

Hi,
I have 2 data frames. I want to assign the values I got from one of the data frame (alpha, epsilon, and lambda) to another based on participant name (X1,X2,X3). There are 180 X1,X2....X30 in the data frame I want to put values in. So I need 180 X1 in the main data frame have a common value of alpha in column alpha from the data frame of the picture below, a common value of epsilon in column epsilon, ect.

'> ori_try <- ori %>%

  • mutate(alpha = res_1$alpha)
    Error: Column alpha must be length 5400 (the number of rows) or one, not 30'

Thank you very much for helping!

Are you trying to do something like this?

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
DF <- data.frame(Name= rep(c("X1", "X2", "X3"), each = 4), Value = 1:12)
DF
#>    Name Value
#> 1    X1     1
#> 2    X1     2
#> 3    X1     3
#> 4    X1     4
#> 5    X2     5
#> 6    X2     6
#> 7    X2     7
#> 8    X2     8
#> 9    X3     9
#> 10   X3    10
#> 11   X3    11
#> 12   X3    12
OtherDF <- data.frame(Name = c("X1", "X2", "X3"), alpha = 1:3, 
                      epsilon = 13:15, lambda = 6:8)
OtherDF
#>   Name alpha epsilon lambda
#> 1   X1     1      13      6
#> 2   X2     2      14      7
#> 3   X3     3      15      8
DF <- inner_join(DF, OtherDF, by = "Name")
DF
#>    Name Value alpha epsilon lambda
#> 1    X1     1     1      13      6
#> 2    X1     2     1      13      6
#> 3    X1     3     1      13      6
#> 4    X1     4     1      13      6
#> 5    X2     5     2      14      7
#> 6    X2     6     2      14      7
#> 7    X2     7     2      14      7
#> 8    X2     8     2      14      7
#> 9    X3     9     3      15      8
#> 10   X3    10     3      15      8
#> 11   X3    11     3      15      8
#> 12   X3    12     3      15      8

Created on 2020-04-24 by the reprex package (v0.3.0)

thank you very much for your help!

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