简体   繁体   中英

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 .

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?

We can use sub on the column names after converting it to a regular data.frame

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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