r比较两个数据框之间的列类型 [英] r compare column types between two dataframes
本文介绍了r比较两个数据框之间的列类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这可能是一个不好的问题,因为我没有发布任何可重复的示例。我的主要目标是识别在两个数据框之间具有不同类型的,具有相同列名的列。
This may be a bad question because I am not posting any reproducible example. My main goal is to identify columns that are of different types between two dataframe that have the same column names.
例如
df1
Id Col1 Col2 Col3
Numeric Factor Integer Date
df2
Id Col1 Col2 Col3
Numeric Numeric Integer Date
在这里,两个数据帧(df1,df2)都具有相同的列名,但Col1类型不同,我对识别此类列感兴趣。预期的输出。
Here both the dataframes (df1, df2) have same column names but the Col1 type is different and I am interested in identifying such columns. Expected output.
Col1 Factor Numeric
有关实现此目标的任何建议或提示?谢谢
Any suggestions or tips on achieving this ?. Thanks
推荐答案
尝试一下:
compareColumns <- function(df1, df2) {
commonNames <- names(df1)[names(df1) %in% names(df2)]
data.frame(Column = commonNames,
df1 = sapply(df1[,commonNames], class),
df2 = sapply(df2[,commonNames], class)) }
这篇关于r比较两个数据框之间的列类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文