Username
Username

Reputation: 3663

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?

Upvotes: 1

Views: 29

Answers (1)

akrun
akrun

Reputation: 887048

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))

Upvotes: 1

Related Questions