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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:13:48+00:00 2026-05-20T05:13:48+00:00

I have a file with two columns, one has the content type of HTTP

  • 0

I have a file with two columns, one has the content type of HTTP objects like text/html, application/rar etc and the other has the bytes size.

Content Type                                     Size
video/x-flv                                       100
image/jpeg                                        150
text/html                                         160
application/octet-stream                          200  
application/x-shockwave-flash                     ...
text/plain
application/x-javascript
text/xml
text/css
text/html; charset=utf-8
application/x-javascript; charset=utf-8           ...

As you can see there are many variations of the same content type, such as application/x-javascript and application/x-javascript; charset=utf-8 and so on. So, I would like to create another column to categorize them more generically. So, that these two would just be web/javascript and so on.

 Content Type                                      Size      Category
    video/x-flv                                       100       web/video
    image/jpeg                                        150       web/image
    text/html                                         160       web/html
    application/octet-stream                          200       web/binary
    application/x-shockwave-flash                     ...       web/flash
    text/plain                                                  web/plaintext
    application/x-javascript                                    web/javascript
    video/x-msvideo                                             web/video
    text/xml                                                    web/xml
    text/css                                                    web/css
    text/html; charset=utf-8                                    web/html
    video/quicktime                                             web/video
    application/x-javascript; charset=utf-8                     web/javascript

How would I accomplish this in R and I presume I need to use regular expressions of some sort for this?

  • 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-20T05:13:48+00:00Added an answer on May 20, 2026 at 5:13 am

    There are several ways you can simplify your variable. Here I will use the stringr package for string manipulation functions :

    R> library(stringr)
    

    First, copy your content type variable into a new character variable :

    R> d <- data.frame(type=c("video/x-flv", "image/jpeg","video/x-msvideo", "application/x-javascript; charset=utf-8", "application/x-javascript"))
    R> d$type2 <- as.character(d$type)
    

    Which just gives you :

                                         type                                   type2
    1                             video/x-flv                             video/x-flv
    2                              image/jpeg                              image/jpeg
    3                         video/x-msvideo                         video/x-msvideo
    4 application/x-javascript; charset=utf-8 application/x-javascript; charset=utf-8
    5                application/x-javascript                application/x-javascript
    

    Then you can work on your new variable. You can just replace manually certain type value by another :

    R> d$type2[d$type2 == "video/x-flv"] <- "video"
    R> d
                                         type                                   type2
    1                             video/x-flv                                   video
    2                              image/jpeg                              image/jpeg
    3                         video/x-msvideo                         video/x-msvideo
    4 application/x-javascript; charset=utf-8 application/x-javascript; charset=utf-8
    5                application/x-javascript                application/x-javascript
    

    You can use regexp matching to replace all the values matching, for example, “video” :

    R> d$type2[str_detect(d$type2, ".*video.*")] <- "video"
    R> d
                                         type                                   type2
    1                             video/x-flv                                   video
    2                              image/jpeg                              image/jpeg
    3                         video/x-msvideo                                   video
    4 application/x-javascript; charset=utf-8 application/x-javascript; charset=utf-8
    5                application/x-javascript                application/x-javascript
    

    Or you can use regexp replacement to clean certain values. For example by removing everything behind the “;” in your content types :

    R> d$type2 <- str_replace(d$type2, ";.*$", "")
    R> d
                                         type                    type2
    1                             video/x-flv                    video
    2                              image/jpeg               image/jpeg
    3                         video/x-msvideo                    video
    4 application/x-javascript; charset=utf-8 application/x-javascript
    5                application/x-javascript application/x-javascript
    

    Be careful of the order of your instructions, though, as your result highly depends on it.

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

Sidebar

Related Questions

I have two columns in a csv file, one of which has the time
I have two text file. file1.txt has: gedit google chrome git vim foo bar
I have a file that has two columns of floating point values. I also
I have a text file that has one colunm of 1800 entries that are
Suppose File 1 has two columns and looks something like: fuzz n. flowering shrub
I have a html file which has a huge table with 4 columns and
Given I have two File objects I can think of the following implementation: public
I have a text file containing two or more types of lines. I would
I have two csv file. First File has date offerid clicks orders Second File
I have a file named file with three lines: line one line two line

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.