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

  • SEARCH
  • Home
  • 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 6921995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:20:56+00:00 2026-05-27T10:20:56+00:00

This question is a followup to my previous question, Importing one long line of

  • 0

This question is a followup to my previous question, Importing one long line of data into R.

I have a large data file consisting of a single line of text. The format resembles

Cat    14         15  Horse  16

I’d eventually like to get it into a data.frame. In the above example I would end up with two variables, two variables, Animal and Number. The number of characters in each “line” is fixed, so in the example above each line contains 11 characters, animals being the first 7 and numbers being the next four.

So what I’d like is a data frame that looks like:

Animal Number
   Cat     14
    NA     15
 Horse     16
  • 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-05-27T10:20:57+00:00Added an answer on May 27, 2026 at 10:20 am

    You can read the file with read.fwf, specifying the column widths and the number of columns:

    inp.fwf <- read.fwf("tmp.txt", widths = rep(c(7, 4), times = 3), as.is = TRUE)
    

    Here the argument times = 3 works for your sample data; for your real file, you’ll have to indicate how many pairs there are and change times accordingly. If you don’t know how many entries you have, this might work:

    inp.rl <- readLines("tmp.txt")
    nchar(inp.rl)/11
    

    This will give you a data.frame with one row and many columns. You need to break that into many rows and two columns:

    inp.mat <- matrix(inp.fwf, byrow = TRUE, ncol = 2)
    

    This will get you the correct shape for your data. The animal names are stored as character vectors, which you’ll probably want to change into factors, but at this point all the data is in R, so you can easily tweak it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a followup on the question: ASP.NET next/previous buttons to display single row
Hey guys this is a followup to my previous question . I now have
So, this is something of a followup to my previous question. I'm running into
This is kind of a followup on my previous question . I have an
This question is a followup to my previous one: Previous Questions . So I
This is a followup to my previous question. Parsing file names from a character
This is a followup of a previous question I had. I got the very
This is a follow up from my previous question I have this code basically
This is somewhat of a followup to my previous question , where people pointed
This question is a follow-up question to my previous one that can be found

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.