用于转换数据框列类型的函数 [英] Function for converting dataframe column type

查看:29
本文介绍了用于转换数据框列类型的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

R 经常以错误"的格式理解数据框列,或者您只需将列类从因子更改为字符即可对其进行修改.我之前一直在通过以下方式更改列类:

R often understands data frame columns in a "wrong" format or you just have to change the column class from factor to character in order to modify it. I have been changing the column class in following way previously:

set.seed(1)

df <- data.frame(x = 1:10,
y = rep(1:2, 5),
k = rnorm(10, 5,2),
z = rep(c(2010, 2012, 2011, 2010, 1999), 2),
j = c(rep(c("a", "b", "c"), 3), "d"))

x <- c("y", "z")

for(i in 1:length(x)){
df[,x[i]] <- factor(df[,x[i]])}

然后回到数字:

x <- 1:5

for(i in 1:length(x)){
df[,x[i]] <- as.numeric(as.character(df[,x[i]]))} # Character cannot become numeric

我突然想到,也许有更好的方法来做到这一点.我发现这个问题,差不多正是我需要的:

It occurred to me that maybe there is a better way doing this. I found this question, which is almost exactly what I need:

convert.magic <- function(obj,types){
out <- lapply(1:length(obj),FUN = function(i){FUN1 <- 
switch(types[i],
character = as.character,
numeric = as.numeric,
factor = as.factor); FUN1(obj[,i])})
names(out) <- colnames(obj)
as.data.frame(out)
}

但是,对于此函数,必须为每一列指定向量类型:

However, for this function vector type has to be specified for each column:

convert.magic(df, rep("factor",5))

convert.magic(df, c("character", "factor"))
# Error in FUN(1:5[[1L]], ...) : could not find function "FUN1"

有人可以帮我重建这个函数,让它与列名和数字一起工作吗?恐怕这对我来说太高级了……

Could somebody help me and rebuild this function so that it works with column names and numbers, please? I am afraid that this would be too advanced for me...

x <- c("y", "z")
convert.magic(df, "character", x)

推荐答案

df <- data.frame(x = 1:10,
                 y = rep(1:2, 5),
                 k = rnorm(10, 5,2),
                 z = rep(c(2010, 2012, 2011, 2010, 1999), 2),
                 j = c(rep(c("a", "b", "c"), 3), "d"))

convert.magic <- function(obj, type){
  FUN1 <- switch(type,
                 character = as.character,
                 numeric = as.numeric,
                 factor = as.factor)
  out <- lapply(obj, FUN1)
  as.data.frame(out)
}

str(df)
str(convert.magic(df, "character"))
str(convert.magic(df, "factor"))
df[, c("x", "y")] <- convert.magic(df[, c("x", "y")], "factor")

这篇关于用于转换数据框列类型的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆