如何使一个data.frame与R中的另一个data.frame一样接受列类型?

如何使一个data.frame与R中的另一个data.frame一样接受列类型?,r,dataframe,casting,R,Dataframe,Casting,请忽略以下所有情况下的打印和缺失: normalize_type <- function(df1,df2) { for (vn in names(df1)) { if (vn %in% colnames(df1) & vn %in% colnames(df2)) { if (typeof(df2[[vn]]) != typeof(df1[[vn]])) { if (typeof(df2[[vn]]) %in% c('double','nu

请忽略以下所有情况下的打印和缺失:

normalize_type <- function(df1,df2) {
  for (vn in names(df1)) {
    if (vn %in% colnames(df1) & vn %in% colnames(df2)) { 
      if (typeof(df2[[vn]]) != typeof(df1[[vn]])) { 
        if (typeof(df2[[vn]]) %in% c('double','numeric')) {
          df1[[vn]] <- as.numeric(df1[[vn]])
          print(paste(vn, "changed to numeric/double"))
        } else if (typeof(df2[[vn]]) == 'integer') {
          df1[[vn]] <- as.integer(df1[[vn]])
          print(paste(vn, "changed to integer"))
        } else if (typeof(df2[[vn]]) == 'factor') {
          df1[[vn]] <- as.factor(df1[[vn]])
          print(paste(vn, "changed to factor"))
        }
       } else {
         print(paste(vn, ": ", typeof(df1[[vn]]), "matched", typeof(df2[[vn]])))
       }
     } else {
       print(paste(vn, "is not in both data frames"))
     }
   }
   df1
 }

normalize_type请提供一个可复制的示例。如果没有更多的上下文,您的代码将无法理解。请提供一个可复制的示例。如果没有更多的上下文,您的代码将无法理解