Meaning of c(…) in RStudio's data browser

2019-08-02 00:31发布

问题:

As you know, when working in RStudio and calling View() in R module, sourcing this module results in opening RStudio's internal data browser window. While most simple data is understandable, I'm confused by seeing data like this: c(NA, NA, NA, 125125, NA). What does it represent? This looks like standard R notation for vectors. However, I expect an embedded data frame in that spot. Would appreciate clarification!

回答1:

c(...) does indeed mean "vector" in RStudio's data browser.. Here's a minimal reproducible example.

set.seed(1)
df1   <- data.frame(id=LETTERS[1:10])
# each element of df1$x is char
df1$x <- sapply(1:10,function(i)do.call(paste,as.list(letters[sample(1:10,5)])))
# each element of df2$x is a vector of char
df2   <- aggregate(x~id,df1,function(x)strsplit(as.character(x)," "))
View(df2)

In the above, each element of df1$x is a character string with 5 random, space-separated characters. The "aggregate" function in this example runs strsplit(...) on each value of df1$x to return a vector of characters, which are stored in the elements of df2$x. So when you View(df2) in RStudio, it reflects the fact that each element of df2$x is a vector.

EDIT (Response to OP's comment)

This creates a dataframe (df1) where each element of df1$z is a dataframe.

df1   <- data.frame(id=LETTERS[1:3])
df    <- data.frame(id=rep(letters[1:3],each=10),x=rnorm(30), y=rnorm(30))
df1$z <- split(df,df$id)
View(df1)

Note that RStudio displays df1$z as a list of vectors, which is, in fact, the underlying structure of a dataframe.