Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8701913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:31:06+00:00 2026-06-13T02:31:06+00:00

So I have the data.frame dat = data.frame(x = c(‘Sir Lancelot the Brave’, ‘King

  • 0

So I have the data.frame

dat = data.frame(x = c('Sir Lancelot the Brave', 'King Arthur',  
                       'The Black Knight', 'The Rabbit'), stringsAsFactors=F)

> dat
                       x
1 Sir Lancelot the Brave
2            King Arthur
3       The Black Knight
4             The Rabbit

And I want to transform it into the data frame

> dat2
                       x    1            2       3      4
1 Sir Lancelot the Brave    Sir   Lancelot     the  Brave
2            King Arthur    King    Arthur
3       The Black Knight    The      Black  Knight 
4             The Rabbit    The     Rabbit

strsplit returns the data as a list

sbt <- strsplit(dat$x, " ")
> sbt
[[1]]
[1] "Sir"      "Lancelot" "the"      "Brave"   

[[2]]
[1] "King"   "Arthur"

[[3]]
[1] "The"    "Black"  "Knight"

[[4]]
[1] "The"    "Rabbit"

and as.data.table does not create NULL values where it should, but repeats values

> t(as.data.table(sbt))
   [,1]   [,2]       [,3]     [,4]    
V1 "Sir"  "Lancelot" "the"    "Brave" 
V2 "King" "Arthur"   "King"   "Arthur"
V3 "The"  "Black"    "Knight" "The"   
V4 "The"  "Rabbit"   "The"    "Rabbit"

I guess I really would like an argument to as.data.table(x, repeat=FALSE), else how can I accomplish this job?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:31:08+00:00Added an answer on June 13, 2026 at 2:31 am

    Here’s one option. The single complication is that you need to first convert each vector to a data.frame with one row, as data.frames are what rbind.fill() expects.

    library(plyr)
    rbind.fill(lapply(sbt, function(X) data.frame(t(X))))
    #     X1       X2     X3    X4
    # 1  Sir Lancelot    the Brave
    # 2 King   Arthur   <NA>  <NA>
    # 3  The    Black Knight  <NA>
    # 4  The   Rabbit   <NA>  <NA>
    

    My own inclination, though, would be to just use base R, like this:

    n <- max(sapply(sbt, length))
    l <- lapply(sbt, function(X) c(X, rep(NA, n - length(X))))
    data.frame(t(do.call(cbind, l)))
    #     X1       X2     X3    X4
    # 1  Sir Lancelot    the Brave
    # 2 King   Arthur   <NA>  <NA>
    # 3  The    Black Knight  <NA>
    # 4  The   Rabbit   <NA>  <NA>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data frame with 3 groups and 3 days: set.seed(10) dat <-
I have a table that looks like: dat = data.frame(expand.grid(x = 1:10, y =
I have the following data frame: > dat V1 V2 1 1 6868 2
Let's say I have the following data frame. dat <- data.frame(city=c(Chelsea,Brent,Bremen,Olathe,Lenexa,Shawnee), tag=c(rep(AlabamaCity,3), rep(KansasCity,3))) I
Let's say I have the following dataset bodysize=rnorm(20,30,2) bodysize=sort(bodysize) survive=c(0,0,0,0,0,1,0,1,0,0,1,1,0,1,1,1,0,1,1,1) dat=as.data.frame(cbind(bodysize,survive)) I'm aware that
I have a data.frame as follows: dat <- structure(list(id = 1:4, date = structure(list(sec
I have a dataframe in R like this: dat = data.frame(Sample = c(1,1,2,2,3), Start
I have following code to draw my logistic distribution: x=seq(-2000,2000,length=1000) dat <- data.frame(x=x) dat$value
I have data frame with some numerical variables and some categorical factor variables. The
I have a data frame with start dates and end dates, along with the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.