在R中将列值从一个数据帧分配给另一个数据帧

时间:2021-03-04 20:10:41

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"abc@gmail.com" 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:

在上面,test_email [1,1]有一个电子邮件ID,如“abc@gmail.com”但是,在赋值后,[1,1]的值为81,而不是电子邮件ID。我无法理解为什么没有分配电子邮件并且分配了一些数字版本。任何人都可以让我知道这个原因以及如何解决。 test_email的结构如下:

structure(list(email = structure(c(81L, 75L, 57L, 61L, 79L, 76L),
.Label = "ajay.bansal@siemens.com", "amanmeet.bhalla@gmail.com",
"aoneshp@gmail.com", "aparna_anand@msn.com", "ar.ashwani@gmail.com",
"ar.parulbansal@gmail.com", "ar.preet02@gmail.com",
"asdawsd@yahoo.com", "assd@yopmail.com",
"avijeet_yadav@rediffmail.com", "avneng1.negi@gmail.com",
"avnihatnagar@yahoo.com", "bansalanuj007@yahoo.com.au",
"bhanu5877@yahoo.co.in"), 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.

我无法找出R在分配期间将电子邮件转换为某些数值的原因。

1 个解决方案

#1


1  

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])

#1


1  

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])