“df []” - “在R做 [英] What does "df[] <-" do in R
本文介绍了“df []” - “在R做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
很简单的问题,我已经在google和stackoverflow中快速搜索。
我在另一篇文章中发现:总计:对于因素。
Pretty simple question, and I have had a quick search in google and stackoverflow. I found this in another post: In aggregate: sum not meaningful for factors.
df[] <- lapply(df, function(x) type.convert(as.character(x)))
df []
work?
推荐答案
它调用 [< - 。data.frame
(即,的数据框架方法[
It invokes [<-.data.frame
(i.e., the data.frame method for [<-
). That way you assign a list to a data.frame. You could also do
df <- as.data.frame(lapply(df, function(x) type.convert(as.character(x))))
示例:
DF <- data.frame(a=1:2, b=3:4)
DF[] <- list(c=10:11, d=12:13)
# a b
# 1 10 12
# 2 11 13
但与此相比:
DF <- `[<-.data.frame`(DF, , , list(c=c("a", "b"), d=c("d", "e")))
# c d
# 1 a d
# 2 b e
VS。这个:
DF <- `[<-.data.frame`(DF, 1:2, 1:2, list(c=c("a", "b"), d=c("d", "e")))
# a b
#1 a d
#2 b e
还有这样:
DF <- as.data.frame(list(c=10:11, d=12:13))
# c d
# 1 10 12
# 2 11 13
这篇关于“df []” - “在R做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文