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

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

问题描述

R常常以错误的格式理解数据帧列,或者您只需要将列类从因子更改为字符以修改它。以前,我一直在改变列类:

  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))$ b $ (i in 1:length(x)){
df [,x [i]]的b
x <-c(y,z)

< - factor(df [,x [i]])}

返回数值:

  x < -  1:5 

for(i in 1:length(x)){
df [,x [i]] < - as.numeric(as.character(df [,x [i]]))}#字符不能变成数字



在我看来,也许有更好的方法来做到这一点。我发现了这个问题,这几乎是正是我所需要的:

  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)

$ / code>

然而,对于这个函数,必须为每一列指定矢量类型:

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

convert.magic(df ,c(character,factor))
#FUN(1:5 [[1L]],...)中的错误:找不到函数FUN1

请问有人可以帮我重建这个函数,这样它就可以处理列名和数字了吗?我恐怕这对我来说太过先进了......

  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( (df,character))
str(convert.magic(df,factor)

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


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

And back to numeric:

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天全站免登陆