简体   繁体   English

如何拆分“data.frame”类型的列?

[英]How do I split a “data.frame” type column?

I have a dataframe df where one of the columns user is itself a data.frame .我有一个 dataframe df其中一个列user本身就是一个data.frame

df <- data.frame(
  user = data.frame(
    id = numeric(),
    name = character()
  )
)

df[nrow(df)+1,] <- c(1,"joe")

How do I split the user column into the id and name columns so that df has the id and name columns instead of the user column?如何将user列拆分为idname列,以便df具有idname列而不是user列?

We can use sub on the column names after converting it to a regular data.frame将其转换为常规 data.frame 后,我们可以在列名上使用sub

df <-  do.call(data.frame, df) 
names(df) <- sub("^user\\.", "", names(df))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM