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L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 2L, 3L, 3L), levels = c("ddd", "eee", "fff"
), class = c("ordered", "factor"))

dftest1 <- 
structure(c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, 1L, NA, NA, NA, 1L, NA, NA, NA, 2L, NA, NA, NA, 
1L, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, 1L, NA, 2L), levels = c("AAA", "BBB"
), class = "factor")

dftest2 <-
structure(c(NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, 1L, NA, 
1L, NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, 
NA, NA, 1L, 1L, NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, 
NA, NA, NA, NA, 1L, NA, NA, NA, 1L, NA, 1L, NA, 1L, NA, NA, NA, 
1L, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, 1L, 1L, 1L), levels = "CCC", class = "factor")

I want to put df1, df2 and df3 (in my case factors) together as placeholders vars2use, so to speak.

var2use <- c(var1, var2, var3)

This placeholder should then be combined with other factors (dftest1, dftest2) into a data set.

This implementation works as expected:

df <- data.frame(dftest1, dftest2, df1, df2, df3)

I was hoping that it could also be implemented in this form:

df <- data.frame(dftest1, dftest2, var2use)

But I get an error:

Error in data.frame(dftest1, dftest2, var2use): 
arguments imply differing number of rows: 82, 3

The background is that I would like to work with placeholders of this type in different places. Does anyone have an idea how to solve this?

Dierforth
  • 81
  • 6
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. So you are saying the `df1` value is a simple factor vector and not a data.frame itself? Normally you could `cbind()` additional columns to create a new data.frame rather than call `data.frame()`. Is there a reason you are storing the variable names in strings rather than just putting the data directory in an object? – MrFlick Jan 09 '23 at 14:33
  • perhaps: https://stackoverflow.com/questions/58431789/create-empty-tibble-data-frame-with-column-names-coming-from-a-vector – Remko Duursma Jan 09 '23 at 14:33
  • @MrFlick I added reproducible examples – Dierforth Jan 09 '23 at 15:28

1 Answers1

1

The more direct way to do this is to just put your values in to a separte data.frame and then cbind() the values when you need them. For example

vars2use <- data.frame(df1, df2, df3)
df <- cbind(data.frame(dftest1, dftest2), vars2use)
# df <- data.frame(dftest1, dftest2) |> cbind(vars2use) # alternative syntax

If for some reason those names really do need to be a character vector, you can use mget() to get the values before using cbind

varnames <- c("df1", "df2", "df3")
df <- cbind(data.frame(dftest1, dftest2), mget(varnames))
MrFlick
  • 195,160
  • 17
  • 277
  • 295