PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, August 28, 2022

[FIXED] How to make loopable string list in R?

 August 28, 2022     csv, r     No comments   

Issue

I am trying to make a minimum example for the data set of my other question. Currently, I have the following which is too complicated and errorprone for having a list of filenames and appending them for full filenames

data = read.csv("/home/masi/dataList.csv",header = FALSE,sep = ",")
i = 1
for (d in data) {
        data[i] = paste("/home/masi/Documents/CSV/", d, sep="")
        i = i + 1
}

dataList.csv

P103C1.csv
P103C2.csv 
P111C1.csv 

Pseudocode to have in columns the thing first and transpose but you still cannot loop it as for (d in data) {...}; and appending once again

data = t(c("P103C1.csv" "P103C2.csv" "P111C1.csv"))

i = 1
for (d in data) {
        data[i] = paste("/home/masi/Documents/CSV/", d, sep="")
        i = i + 1
}

Actually, I think having the transpose there is not a good idea and a better and simpler method should exist.

Expected output: something equivalent to data = read.csv("/home/masi/dataList.csv",header = FALSE,sep = ",").

OS: Debian 8.5
R: 3.1.1


Solution

You don't need to create a string in this case as you already have all the files in datalist.csv. Create a list out of datalist.csv and then feed into a loop.

listoffiles=list("P103C1.csv","P103C2.csv","P111C1.csv")
files=list()

for(i in 1:length(listoffiles)){
    files[[i]]=read.csv(listoffiles[[i]], header = F, sep = ',')
    }


Answered By - Chirayu Chamoli
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing