0

I have two dataframes which look like follows:

df1 <- data.frame(V1 = 1:4, V2 = rep(2, 4), V3 = 7:4)
df2 <- data.frame(V2 = rep(NA, 4), V1 = rep(NA, 4), V3 = rep(NA, 4))

I need to write a function which assigns the values of df1 to df2, if the columnnames of both dataframes are the same. The structure of the function should look like this:

fun <- function(x){
  if(# If the name of x is the same like the name of a column in df1)
    out <- df1$? # Here I need to assign df1$"x" somehow
    out
}

fun(df2$V1)

The output should look like this:

[1] 1 2 3 4

Unfortunately I couldnt find a solution by myself. Is there a way how I could do this? Thank you very much in advance!

3 Answers 3

3

I need to write a function which assigns the values of df1 to df2, if the columnnames of both dataframes are the same.

Are you sure you need a function?

names_in_common <- intersect(names(df1),names(df2))

df2[,names_in_common] <- df1[,names_in_common]
Sign up to request clarification or add additional context in comments.

1 Comment

The problem with both answers is that I might need to do it for a single vector of a dataframe and names doesnt give me the name of the single input. If I try names(df2$V1) it returns "NULL". Is there a way how I could get the name of this single vector?
2

Using Joachim Schork's code:

names_in_common <- intersect(names(df1),names(df2))

df2[,names_in_common] <- df1[,names_in_common]

and if you want to change a single column of df2:

names_in_common <- intersect(names(df1), names(df2[, "V1", drop=FALSE]))

df2[,names_in_common] <- df1[,names_in_common]

Comments

1

This is impossible, because when you access a column of a data.frame using the dollar syntax you lose the column name. There's no way for fun() to determine the column name of the vector that was passed in as an argument.

Instead, you can simply call fun() using the column name itself as the argument, rather than the vector of NAs, which are not useful and not used at all inside the function. In other words, the call becomes

fun('V1');

Then you can write the function as follows:

fun <- function(name) df1[[name]];

Demo:

fun('V1');
## [1] 1 2 3 4

Although now that I think about it, you might as well just index df1 directly, since that's all the function does now:

df1$V1;
## [1] 1 2 3 4

Rereading your question, you said you want to assign the column from df1 to df2, although your example code doesn't do that. Assuming you did want to carry out this assignment inside the function, you could do this:

fun <- function(name) df2[[name]] <<- df1[[name]];

Demo:

fun('V1');
df2;
##   V2 V1 V3
## 1 NA  1 NA
## 2 NA  2 NA
## 3 NA  3 NA
## 4 NA  4 NA

This makes use of the superassignment operator <<-.

1 Comment

Thanks a lot to everyone who answered the question! Based on the answers Ive managed to find a solution. Like in bgoldsts solution I used a character as input for the function, e.g. "V1".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.