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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:45:14+00:00 2026-06-16T10:45:14+00:00

I have been trying to import several csv files, use the function melt and

  • 0

I have been trying to import several csv files, use the function “melt” and merge them into a single database in R. All the files have an “id”, “date.time” and “tag” column; however, the rest of the columns differ among files. This is an example of a few lines in one the file:

               date.time   tag 111015 111016 113949 113950
1 1 2012-10-11 00:00:00 14767      0      0      0      0
2 2 2012-10-11 01:00:00 14767      0      0      0      0
3 3 2012-10-11 02:00:00 14767      0      0      0      0
4 4 2012-10-11 03:00:00 14767      0      0      0      0
5 5 2012-10-11 04:00:00 14767      0      0      0      0
6 6 2012-10-11 05:00:00 14767      0      0      0      0

library(reshape2)

# Import files

files<-list.files()
data<-lapply(files,read.csv,header=TRUE,sep=",",check.names=FALSE)

I am trying to use this loop to melt each file and bind the resulting data frame. However, its only working for the last file in the loop. I don’t know exactly how to set the loop/function so that it can perform first the “melt” of each file and them “merge/bind” them into a single data frame.

for(j in 1:length(data)){
   dm<-melt(data[[j]],measure.vars=c(4:length(data[[j]])),
     id=c("date.time","tag"),variable.name="receiver")

   results<-rbind(dm)   

  }

Any suggestions will be appreciated!

  • 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-16T10:45:15+00:00Added an answer on June 16, 2026 at 10:45 am

    Its better to do using lapply to load everything first and then use melt as follows: (Assuming all your files are in the variable files,

    Note: Untested 
    require(reshape2)
    files <- list.files(my.dir, full.names = TRUE)
    # first load all files
    dd <- lapply(1:length(files), function(idx) {
        d <-read.csv(files[idx], header = TRUE, sep=",", check.names = FALSE)
        # if you want the file index
        d$file.idx <- idx
        d
    })
    # merge all
    dd <- do.call(rbind, dd)
    # now melt
    dd.m <- melt(dd, c(4:length(d)), c("date.time","tag"), variable.name = "receiver")
    

    Edit: After Op’s edit

    Note: Untested 
    require(reshape2)
    files <- list.files(my.dir, full.names = TRUE)
    dd.m <- lapply(1:length(files), function(idx) {
        # load the file
        d <-read.csv(files[idx], header = TRUE, sep=",", check.names = FALSE)
        # now melt immediately
        d.m <- melt(d, c("date.time","tag"), c(4:length(d)))
    })
    # merge all
    dd.m <- do.call(rbind, dd.m)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to use 'import poplib' to access gmail, since I have Pop
I have been trying to import Database through phpMyAdmin. My database file is a.sql
I have been trying to import iostream into a custom block, I added the
I have been trying to import this excel file my mysql database, and it
I am trying to import several .csv files that contain a few fields with
Have have been trying to make a validator for my xml files. I have
I have been trying for almost a week now to create an SQLite database
I have been trying for quite a while to import a large (4GB) sql
We have been trying to modify the Customer.php file ( import/export ) to get
I have been trying to use cx_freeze for some time now, and yet 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.