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 8908165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:06:36+00:00 2026-06-15T03:06:36+00:00

I have the following like text (csv) files to read, where first four columns

  • 0

I have the following like text (csv) files to read, where first four columns are of my interest but after that I have alot of junk. I just want to read first four columns into R.

enter image description here

I want first four columns, so that output (csv opened in excel) would look like:

enter image description here

I could not paste the whole file nor attach it due to limitations of SO. Here is smaller example for workout:

type,latitude,longitude,name,link1,
W,43.075319,-89.386145,Mirch Masala,"<just link, jjksskkls  hskks > ","<just link, jjksskkls  hskks > "
W,43.07488,-89.390698,Himal Chuli Restaurant,"<just link, jjksskkls  hskks > ","<just link,  hskks , hsksks  > "
W,43.074887,-89.391011,Chautara Restaurant,"<just link,  hskks , hsksks  > ","<just link, jjksskkls  hskks > "
W,43.092866,-89.351587,Dobhan Restaurant,"<just link, jjksskkls  hskks > ","<just link, jjksskkls , ssjjs hskks > "
W,43.074746,-89.393137,State Street Cash Mart,"<just link, jjksskkls  hskks > ","<just link, jjksskkls , ssjjs hskks > "
W,43.072801,-89.395718,Dotty Dumplings Dowry,"<just link, jjksskkls ,    hskks > ","<just link, jjksskkls , ssjjs hskks > "
W,43.074744,-89.393046,Dobra Tea,"<just link, jjksskkls  hskks > ","<just link, jjksskkls , ssjjs hskks > "
W,43.076372,-89.380231,Hi-Madison,"<just link, jjksskkls  hskks > ","<just link, jjksskkls , ssjjs hskks > "
W,43.019624,-89.421822,Candlewood Suites Fitchburg,"<just link, jjksskkls , ssjjs hskks > ","<just link, jjksskkls , ssjjs hskks > "
W,43.08154,-89.524094,Holiday Inn Hotel & Suites Madison West,"<just link, jjksskkls 100  hskks > ","<just link, jjksskkls , ssjjs hskks > "

Any idea on just reading first four columns while importing into R ?

  • 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-15T03:06:37+00:00Added an answer on June 15, 2026 at 3:06 am

    Based on your comments to your question, your title is somewhat misleading. Where you’re running into problems is not knowing the exact number of columns your final data.frame would have.

    From the ?read.table help page:

    count.fields can be useful to determine problems with reading files which result in reports of incorrect record lengths

    So, let’s try a different answer.

    First, let this represent your data:

    "W",43.075319,-89.386145,"Mirch Masala","<J, K>"
    "W",43.07488,-89.390698,"Himal Chuli Restaurant","<J, K>","<J, K>","<J, K>"
    "W",43.074887,-89.391011,"Chautara Restaurant","<J, K>","<J, K>"
    "W",43.092866,-89.351587,"Dobhan Restaurant","<J, K>","<J, K>","<J, K>","<J, K>"
    "W",43.074746,-89.393137,"State Street Cash Mart","<J, K>"
    "W",43.072801,-89.395718,"Dotty Dumplings Dowry"
    

    (This next step won’t be required from your side if this is already saved as a text or csv file, but for the sake of a minimal reproducible example…)

    Write those lines to a text file to simulate the read.table process:

    writeLines('"W",43.075319,-89.386145,"Mirch Masala","<J, K>"
    "W",43.07488,-89.390698,"Himal Chuli Restaurant","<J, K>","<J, K>","<J, K>"
    "W",43.074887,-89.391011,"Chautara Restaurant","<J, K>","<J, K>"
    "W",43.092866,-89.351587,"Dobhan Restaurant","<J, K>","<J, K>","<J, K>","<J, K>"
    "W",43.074746,-89.393137,"State Street Cash Mart","<J, K>"
    "W",43.072801,-89.395718,"Dotty Dumplings Dowry"', "myRaggedFile.txt")
    

    That will create a “ragged” file to be read in using read.table or read.csv. The trick, though, is to use count.fields to figure out how many columns the file should have.

    dat <- read.csv("myRaggedFile.txt", header=FALSE, 
                    col.names=1:max(count.fields("myRaggedFile.txt", sep=",")))
    dat
    #      X1       X2        X3                     X4     X5     X6     X7     X8
    # 1     W 43.07532 -89.38614           Mirch Masala <J, K>                     
    # 2     W 43.07488 -89.39070 Himal Chuli Restaurant <J, K> <J, K> <J, K>       
    # 3     W 43.07489 -89.39101    Chautara Restaurant <J, K> <J, K>              
    # 4     W 43.09287 -89.35159      Dobhan Restaurant <J, K> <J, K> <J, K> <J, K>
    # 5     W 43.07475 -89.39314 State Street Cash Mart <J, K>                     
    # 6     W 43.07280 -89.39572  Dotty Dumplings Dowry            
    dat <- dat[1:4] # To keep just the first four columns
    ## Or, continuing with my original answer:
    ## read.csv("myRaggedFile.txt", header=FALSE, 
    ##          col.names=1:max(count.fields("myRaggedFile.txt", sep=",")))[1:4]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have two text inputs like the following, i don't want to use <form>
I have text documents like the following which contain single and multiple variables: title::
I have the following text: We%27re%20proud%20to%20introduce%20the%20Amazing I'd like to remove the encoding using PHP,
I have the following table and would like to replace the string text of
Supposed I have the following string: string str = <tag>text</tag>; And I would like
I have a text file containing words separated by newline , like the following
I have an .erb file where I'd like to return the following text: Name
I have some CSV data files that I want to import into mySQL. I
I have a script that generates a csv file using the following code: header('Content-type:
I have text in the form of separate lines, where each line has CSV-like

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.