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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:31:22+00:00 2026-06-16T00:31:22+00:00

I would like to take a data frame with characters and numbers, and concatenate

  • 0

I would like to take a data frame with characters and numbers, and concatenate all of the elements of the each row into a single string, which would be stored as a single element in a vector. As an example, I make a data frame of letters and numbers, and then I would like to concatenate the first row via the paste function, and hopefully return the value “A1”

df <- data.frame(letters = LETTERS[1:5], numbers = 1:5)
df

##   letters numbers
## 1       A       1
## 2       B       2
## 3       C       3
## 4       D       4
## 5       E       5

paste(df[1,], sep =".")
## [1] "1" "1"

So paste is converting each element of the row into an integer that corresponds to the ‘index of the corresponding level’ as if it were a factor, and it keeps it a vector of length two. (I know/believe that factors that are coerced to be characters behave in this way, but as R is not storing df[1,] as a factor at all (tested by is.factor(), I can’t verify that it is actually an index for a level)

is.factor(df[1,])
## [1] FALSE
is.vector(df[1,])
## [1] FALSE

So if it is not a vector then it makes sense that it is behaving oddly, but I can’t coerce it into a vector

> is.vector(as.vector(df[1,]))
[1] FALSE

Using as.character did not seem to help in my attempts

Can anyone explain this behavior?

  • 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-16T00:31:22+00:00Added an answer on June 16, 2026 at 12:31 am

    While others have focused on why your code isn’t working and how to improve it, I’m going to try and focus more on getting the result you want. From your description, it seems you can readily achieve what you want using paste:

    df <- data.frame(letters = LETTERS[1:5], numbers = 1:5, stringsAsFactors=FALSE)
    paste(df$letters, df$numbers, sep=""))
    
    ## [1] "A1" "B2" "C3" "D4" "E5"
    

    You can change df$letters to character using df$letters <- as.character(df$letters) if you don’t want to use the stringsAsFactors argument.

    But let’s assume that’s not what you want. Let’s assume you have hundreds of columns and you want to paste them all together. We can do that with your minimal example too:

    df_args <- c(df, sep="")
    do.call(paste, df_args)
    
    ## [1] "A1" "B2" "C3" "D4" "E5"
    

    EDIT: Alternative method and explanation:

    I realised the problem you’re having is a combination of the fact that you’re using a factor and that you’re using the sep argument instead of collapse (as @adibender picked up). The difference is that sep gives the separator between two separate vectors and collapse gives separators within a vector. When you use df[1,], you supply a single vector to paste and hence you must use the collapse argument. Using your idea of getting every row and concatenating them, the following line of code will do exactly what you want:

    apply(df, 1, paste, collapse="")
    

    Ok, now for the explanations:

    Why won’t as.list work?

    as.list converts an object to a list. So it does work. It will convert your dataframe to a list and subsequently ignore the sep="" argument. c combines objects together. Technically, a dataframe is just a list where every column is an element and all elements have to have the same length. So when I combine it with sep="", it just becomes a regular list with the columns of the dataframe as elements.

    Why use do.call?

    do.call allows you to call a function using a named list as its arguments. You can’t just throw the list straight into paste, because it doesn’t like dataframes. It’s designed for concatenating vectors. So remember that dfargs is a list containing a vector of letters, a vector of numbers and sep which is a length 1 vector containing only “”. When I use do.call, the resulting paste function is essentially paste(letters, numbers, sep).
    But what if my original dataframe had columns "letters", "numbers", "squigs", "blargs" after which I added the separator like I did before? Then the paste function through do.call would look like:

    paste(letters, numbers, squigs, blargs, sep)
    

    So you see it works for any number of columns.

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

Sidebar

Related Questions

I would like to take a field and replace all characters that are not
I'd like to write some code that would take a given data frame, check
Take the Winlogon registry section, I would like PowerShell to display the Data value
I would like to take a string representation of a set and parse it.
Using Zend_Dom_Query, I would like to retrieve meta data out of an HTML string.
I would like to take the data output from the query below and join
I would like to aggregate a data frame by time interval, applying a different
I would like to take this data: questionX Where X is any number from
I would like to take the Roman Numeral value stored in the object and
I would like to take a specific part of the last line of a

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.