logo
down
shadow

Importing a similar column (eg. pH column) from 60 csv files thru use of loop and making a matrix .


Importing a similar column (eg. pH column) from 60 csv files thru use of loop and making a matrix .

By : mercho
Date : November 21 2020, 11:01 PM
I wish this help you I want to create a loop that reads a specific column (Lets say pH) in a directory of 60 csv files and write that column in csv file next to each other as the order of files in the directory.
code :
directory <- "C:/"
file_name <- list.files(directory, pattern = ".csv")
files.to.read <- paste(directory,  file_name, sep="/")
x<-list()
for (i in 1:length(files.to.read)) {
  dta <- read.csv(files.to.read[i], header=TRUE)
  x[[i]]<-dta$pH
}
ListLength<-NULL


for (i in 1:length(x)){
  ListLength<-c(ListLength, length(x[[i]]))
}
ManyNAs<-max(ListLength)-ListLength
OutputDF<-data.frame(matrix(NA, nrow = max(ListLength), ncol = length(file_name)))
for (i in 1:length(x)){
  OutputDF[,i]<-c(x[[i]],rep(NA, ManyNAs[i]))

}

write.csv(OutputDF, "OutputDF.csv")
for (i in 1:length(x)){
   OutputDF[,i]<-c(x[[i]],rep("",ManyNAs[i]))
}


Share : facebook icon twitter icon
How to perform a column by column circular shift of a matrix without a loop

How to perform a column by column circular shift of a matrix without a loop


By : Ilche Ivanovski
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Edit
Pursuit is right: Using a for-loop and appropriate indexing seems to be the way to go here. Here's one way of doing it:
code :
[m, n] = size(A);
D = randi([0, m - 1], [1, n]);
B = zeros(m, n);

for i = (1 : n)
    B(:, i) = [A((m - D(i) + 1 : m), i); A((1 : m - D(i) ), i)];
end
[m, n] = size(A);
mtxLinearIndices ...
    = bsxfun(@plus, ...
             mod(bsxfun(@minus, (0 : m - 1)', D), m), ...
             (1 : m : m * n));
C = A(idxs);
Importing multiple csv files and extracting one column of data to form a single matrix

Importing multiple csv files and extracting one column of data to form a single matrix


By : Madhu kakarla
Date : March 29 2020, 07:55 AM
will be helpful for those in need I am very new to MATLAB, so I am fairly certain that this is a very simple question. I have several output data sets, each with a prefix (e.g. PT_1 to PT_20). I would like to use a for loop to import data from the second column of each of the csv file to a new matrix and align it with time, which is constant in all files. , Try this:
code :
%# Set the number of csv files
DirectoryPath = 'FullDirectoryPathHereWithTrailingSlash';
NumFile = 2;

%# Open the first file and get the first column (the date column)
File1Path = [DirectoryPath, 'PT_1.csv'];
fid1 = fopen(File1Path, 'r');
Date = textscan(fid1, '%s %*[^\n]', 'Delimiter', ',', 'HeaderLines', 1);
fclose(fid1);

%Convert dates to matlab date numbers and get number of rows
Date = datenum(Date{1, 1}, 'dd/mm/yyyy');
T = size(Date, 1);

%# Preallocate a matrix to hold all the data, and add the date column
D = [Date, NaN(T, NumFile)];

%# Loop over the csv files, get the second column and add it to the data matrix
for n = 1:NumFile

    %# Get the current file name
    CurFilePath = [DirectoryPath, 'PT_', num2str(n), '.csv'];

    %# Open the current file for reading and scan in the second colum using numerical format
    fid1 = fopen(CurFilePath, 'r');
    CurData = textscan(fid1, '%*s %f %*[^\n]', 'Delimiter', ',', 'HeaderLines', 1);
    fclose(fid1);

    %Add the current data to the cell array
    D(:, n+1) = CurData{1, 1};

end
time, param 1, param 2, param 3
2/01/2001 23:00, 11, 3, 314.322471
3/01/2001 23:00, 12, 3, 311.683002
time, param 1, param 2, param 3
2/01/2001 23:00, 13, 0, 296.523937
3/01/2001 23:00, 14, 0, 294.0944
>> D

D =

      730853          11          13
      730854          12          14
How to convert single column data into two-column matrix using conditional/for loop in R

How to convert single column data into two-column matrix using conditional/for loop in R


By : kaleem yousafzai
Date : March 29 2020, 07:55 AM
should help you out Although I will stick with packages, here is a solution
initialize data
code :
mydf <- data.frame(x=c(">PROKKA_00002 Alpha-ketoglutarate","MTESSITERGAPEL", "MTESSITERGAPEL",">PROKKA_00003 lipoprotein", "MTESSITERGAPEL"   ,"MRTIIVIASLLLT"), stringsAsFactors = F)
ind <- grep(">", mydf$x)
temp<-data.frame(ind=ind, from=ind+1, to=c((ind-1)[-1], nrow(mydf)))

seqs<-rep(NA, length(ind))
for(i in 1:length(ind)) {
  seqs[i]<-paste(mydf$x[temp$from[i]:temp$to[i]], collapse="")
}

fastatable<-data.frame(name=gsub(">", "", mydf[ind,1]), sequence=seqs)


> fastatable
                              name                     sequence
1 PROKKA_00002 Alpha-ketoglutarate MTESSITERGAPELMTESSITERGAPEL
2         PROKKA_00003 lipoprotein  MTESSITERGAPELMRTIIVIASLLLT
AWK: finding common elements across arbitrary number of columns (either single column files or column matrix)

AWK: finding common elements across arbitrary number of columns (either single column files or column matrix)


By : Júlio Leitão Jr.
Date : March 29 2020, 07:55 AM
should help you out Problem ,
to find what elements are contained across all files
code :
#Store the files in an array. Assuming all files in one place
filelist=( $(find . -maxdepth 1 -type f) ) #array of files
awk -v count="${#filelist[@]}" '{value[$1]++}END{for(i in value){
if(value[i]==count){printf "Value %d is found in all files\n",i}}}' "${filelist[@]}"
Is it possible to multiply all of each column in matrix A by each column of matrix B without for loop?

Is it possible to multiply all of each column in matrix A by each column of matrix B without for loop?


By : de_dust
Date : March 29 2020, 07:55 AM
like below fixes the issue Seems like a perfect fit for np.einsum, as we need to the keep the first axis aligned between the two inputs and keep it in the output -
code :
np.einsum('ij,ik->ki',V,W)
In [2]: W = np.random.randint(4,6, size=(6, 4))
   ...: H = np.random.randint(1,3, size=(4, 5))
   ...: V = np.dot(W,H) + 1    
   ...: keep = np.array([]).reshape(0,6)
   ...: 

In [5]: group = W.shape[1]
   ...: for k in xrange(group):
   ...:     # multiply all of each column of V by k-th column of W and sum in row
   ...:     keep = np.vstack([keep, sum(V[:,:].T*W[:,k])])
   ...:     

In [6]: np.allclose(keep, np.einsum('ij,ik->ki',V,W))
Out[6]: True
Related Posts Related Posts :
  • geom_text how to position the text on bar as I want?
  • How to escape the symbol of percentage '%' in r?
  • How to get R plot to plot variable on heat.color scale
  • Dynamically selecting principal components from the PCA output
  • Replicate a regression using a random subset of data each time and check distribution of regression coefficients?
  • How to find a string in a vector in r?
  • How to replace outliers with NA having a particular range of values in R?
  • rearrange data: convert from water year to calendar year
  • Plot multiple graphs on one plot in R within loop
  • Unconditional Probability Calculation in R
  • Calculate & add column length_of_time from date column in R
  • Asterisk in ggplot's annotate()
  • How to copy a row from a data frame to a matrix in R?
  • Reading text file with multi-char delimiter to a list in R
  • ggplot2 scale colours for heatmap
  • Looking to save a 3D spectrograph generated by SeeWave as .STL or similar in R
  • Separating three dimensional array by the stratifying variable
  • "Object '...' not found when referenced object is a nested function in R
  • How to correctly clearShapes in Leaflet in a Shiny app
  • NetLogo 5.3.1 and R 3.3.1 on Mac 10.10.6 headless exception
  • Extract unique values from column in dataframe, grouped by ID
  • Stacked Bar Chat (ggplot) in R:: Trying to create more than one bar in the chart
  • R: Creating a function to merge two columns
  • Error in knearest(Darr, P_set, K) : object 'knD' not found
  • Determining least significant predictor in Linear Regression Model with R
  • listcolumns and multidplyr
  • How to check for the presence of a location in a sentence in R?
  • Using mapply over a matrix when the function makes use of elemets' location in the matrix
  • R - stratified sampling for Person Period file
  • Merging two xts to create NAs at certain time stamps
  • How to subset data in a loop in R?
  • How to use sprintf to show string "\t"?
  • Subset Columns based on partial matching of column names in the same data frame
  • R Extract number from string
  • How to put 2 boxplot in one graph in R without additional libraries?
  • ggplot omits polygon holes
  • ggplot2 geom_boxplot side by side with facets
  • outlier labels incorrectly assigned with ggplot2 box plot
  • capture column pattern frequency
  • R remove tags starting with U
  • R, dplyr and snow: how to parallelize functions which use dplyr
  • R: Separate String Without Delimiter (like Fixed Width in Excel)
  • In R, is there a parsimonious or efficient way to get a regression prediction holding all covariates at their means?
  • Determine mode locations of the kernel density estimate of multimodal univariate data
  • Splitting a column ( column A with numbers) into sub columns with columns names as
  • Which calculation of 2 to the 168th power is correct, and why?
  • 403 Error When Using Rvest to Log Into Website For Scraping
  • R ggplot2 doesn't know a custom function
  • Shiny - Where can I find ObserveEvent
  • decreasing coefficients in R's coefplot?
  • Summing nearby elements of a matrix in R
  • Unequal number of elements when webscraping
  • Reshape data into long columns based on variable in R for IRR
  • ARIMA modelling, prediction and plotting with CO2 dataset in R
  • modelr: cross-validated model fitting for all variables in a dataset that match criterion
  • How to draw a shape (ellipse or oval) following some points and calculate its area?
  • Create new column with percentages in data frame
  • Fitting ARIMA model to multiple time series and storing forecast into a matrix
  • Tidy multivariate data in R
  • R - difference scatter plot
  • shadow
    Privacy Policy - Terms - Contact Us © soohba.com