数据框内的数据框? [英] Dataframe within dataframe?

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

问题描述

请考虑以下示例:

df <- data.frame(id=1:10,var1=LETTERS[1:10],var2=LETTERS[6:15])

fun.split <- function(x) tolower(as.character(x))
df$new.letters <- apply(df[ ,2:3],2,fun.split)

df$new.letters.var1
#NULL

colnames(df)
# [1] "id"          "var1"        "var2"        "new.letters"

df$new.letters
#       var1 var2
# [1,]  "a"  "f" 
# [2,]  "b"  "g" 
# [3,]  "c"  "h" 
# [4,]  "d"  "i" 
# [5,]  "e"  "j" 
# [6,]  "f"  "k" 
# [7,]  "g"  "l" 
# [8,]  "h"  "m" 
# [9,]  "i"  "n" 
# [10,] "j"  "o" 

会这样一个好心人,并解释这里发生了什么吗?

Would be someone so kind and explain what is going on here? A new dataframe within dataframe?

我期望这样:

colnames(df)
# id var1 var2 new.letters.var1 new.letters.var2


推荐答案

原因是因为您通过 apply matrix 输出c $ c>。因此,结果将是在单个列中的 matrix 。您可以使用

The reason is because you assigned a single new column to a 2 column matrix output by apply. So, the result will be a matrix in a single column. You can convert it back to normal data.frame with

 do.call(data.frame, df)

更直接的方法是分配2列,我使用 lapply ,而不是应用,因为在某些情况下,列属于不同的类。 apply 返回一个矩阵,并且混合类别时,列将为字符类别。但是, lapply list 中获得输出,并保留 class

A more straightforward method will be to assign 2 columns and I use lapply instead of apply as there can be cases where the columns are of different classes. apply returns a matrix and with mixed class, the columns will be 'character' class. But, lapply gets the output in a list and preserves the class

df[paste0('new.letters', names(df)[2:3])] <- lapply(df[2:3], fun.split)

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

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