Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning a column value from one data frame to another in R

Tags:

r

I have created an empty data frame using another data frame with the below code.

compare<-data.frame(nrow=nrow(test_email),ncol=ncol(test_data))
colnames(compare)<-c("email", "gender")

Now, I am trying to assign value to the columns of compare data frame based on some conditions using simple assignment statement.

compare[1,1]<-test_email[1,1]
compare[1,2]<-test_data[1,2]

In the above, test_email[1,1] has an email ID like"[email protected]" But, after assignment compare[1,1] has value 81 and not the email ID. I am not able to get it why the email is not getting assigned and some numeric vlaue is getting assigned. Can anyone let me know this reason and how to solve. Structure of test_email is below:

structure(list(email = structure(c(81L, 75L, 57L, 61L, 79L, 76L),
.Label = "[email protected]", "[email protected]",
"[email protected]", "[email protected]", "[email protected]",
"[email protected]", "[email protected]",
"[email protected]", "[email protected]",
"[email protected]", "[email protected]",
"[email protected]", "[email protected]",
"[email protected]"), class = "factor")), .Names = "email",
w.names = c(NA, 6L), class = "data.frame")

I am not able to find out why R is converting email into some numeric values during assignment.

like image 200
Kunal Batra Avatar asked Jan 25 '26 12:01

Kunal Batra


1 Answers

Your assignment is still reading the email addresses as factors.

A simple approach to this would be:

compare[1,1] <- as.character(test_email[1,1])
like image 114
Abir Majumdar Avatar answered Jan 28 '26 02:01

Abir Majumdar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!