使用另一个参考数据框更改数据框中的变量类别 [英] Change class of variables in a data frame using another reference data frame
问题描述
我正在寻找一种方法,通过使用另一个数据帧的引用来更改一个数据帧中的变量类别,该数据帧具有每个变量的类信息。
I was looking for some way to change class of variables in one data frame by using the reference of another data frame which has information of class for each variable.
我有一个包含大约150个变量的数据。所有变量均为字符格式。现在,我想根据其类型更改每个变量的类。为此,我们创建了一个单独的数据框,其中包含每个变量的类信息。让我用一个示例数据框来解释。
I have a data which contains around 150 variables. All the variables are in character format. Now I want to change the class of each variable depending upon its type. For this we created a separate data frame having information of class for each of the variables. Let me explain with an sample data frame.
考虑我的原始数据框是具有5个变量的df-
Consider my original data frame to be df with 5 variables -
df <- data.frame(A="a",B="1",C="111111",D="d",E="e")
现在,我们有了另一个数据框 variable_info,其中仅包含2个变量,一个 variable_name和另一个 variable_class 。
Now we have another data frame "variable_info" which contains just 2 variables, one "variable_name" and another "variable_class".
variable_info <- data.frame(variable_name=c("A","B","C","D","E"),variable_class=c("character","integer","numeric","character","character"))
现在使用variable_info数据框,我想更改df中每个变量的类,以便它们的类如 variable_info $ variable_class中指定的那样,将变量名与 variable_info $ variable_name
Now using the variable_info data frame I want to change the class for each of the variables in df so that their class is as specified in "variable_info$variable_class" linking the variable name with "variable_info$variable_name"
如何为数据框执行此操作?在data.table中这样做会很好吗?
How can we do this for a data frame? Will it be good to do this in data.table? How can we do this in data.table?
谢谢!
Prasad
推荐答案
您可以尝试如下操作:
确保两个表都位于同一表中顺序:
Make sure both tables are in the same order:
variable_info <- variable_info[match(variable_info$variable_name, names(df)),]
创建函数调用列表:
funs <- sapply(paste0("as.", variable_info$variable_class), match.fun)
然后将它们映射到每一列:
Then map them to each column:
df[] <- Map(function(dd, f) f(as.character(dd)), df, funs)
使用 data.table
可以执行几乎相同的操作,只是将最后一行替换为:
With data.table
you could do it almost the same way, except you replace the last line by:
library(data.table)
dt <- as.data.table(df) # or use setDT(df)
dt[, names(dt) := Map(function(dd, f) f(as.character(dd)), dt, funs)]
这篇关于使用另一个参考数据框更改数据框中的变量类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!