方括号“ []”将R中的输出从列表更改为数据框有什么作用?

时间:2019-05-12 23:06:52

标签: r operators lapply

与lapply一起使用时,方括号“ []”使数据框保持为数据框。但是,如果不使用方括号,则代码将创建一个列表。为什么会这样?似乎[]是一个空索引。是否用等式右侧的内容替换数据框的内容?

df <- data.frame("Col1" = c(1,2),"Col2" = c(3,4))
str(df)
# [] below keeps the values in a dataframe
df[] <- lapply(df,as.character)
str(df)
# without [], df becomes a list
df <- lapply(df,as.character)
str(df)
# I understand what [] is doing above, but why does it do this?

0 个答案:

没有答案