logo
down
shadow

R - Automatically stack every nth column of a data frame and save them as new objects


R - Automatically stack every nth column of a data frame and save them as new objects

By : Ike A.
Date : November 22 2020, 03:01 PM
hop of those help? Here are some alternatives. No packages are used.
1) aperm Create a 3d array a, permute the dimensions and reshape into a matrix m and then convert that to a data frame. This one only works if all values are of the same type. (2) and (3) do not have this limitation.
code :
k <- 3
nr <- nrow(DF)
nc <- ncol(DF)
unames <- unique(names(DF))

a <- array(as.matrix(DF), c(nr, k, nc/k))
m <- matrix(aperm(a, c(1, 3, 2)),, k, dimnames = list(NULL, unames))
as.data.frame(m, stringsAsFactors = FALSE)
   A  B  C
1 a1 b1 c1
2 a2 b2 c2
3 a3 b3 c3
4 a4 b4 c4
5 a5 b5 c5
6 a6 b6 c6
7 a7 b7 c7
8 a8 b8 c8
unames <- unique(sub("\\d*$", "", names(DF2)))
L <- lapply(split(as.list(DF), names(DF)), unlist)
as.data.frame(L, stringsAsFactors = FALSE)
   A  B  C
1 a1 b1 c1
2 a2 b2 c2
3 a3 b3 c3
4 a4 b4 c4
5 a5 b5 c5
6 a6 b6 c6
7 a7 b7 c7
8 a8 b8 c8
names0 <- sub("\\d*$", "", names(DF2))   # names without the trailing digits
L <- lapply(split(as.list(DF2), names0), unlist)
as.data.frame(L, stringsAsFactors = FALSE)
varying <- split(1:nc, names(DF))
reshape(setNames(DF, 1:nc), dir = "long", varying = varying, v.names = unames)
    time  A  B  C id
1.1    1 a1 b1 c1  1
2.1    1 a2 b2 c2  2
3.1    1 a3 b3 c3  3
4.1    1 a4 b4 c4  4
1.2    2 a5 b5 c5  1
2.2    2 a6 b6 c6  2
3.2    2 a7 b7 c7  3
4.2    2 a8 b8 c8  4
reshape(DF2, dir = "long", varying = TRUE, v.names = unique(names0))
Lines <- "      A      B      C      A      B      C 
1    a1     b1     c1     a5     b5     c5
2    a2     b2     c2     a6     b6     c6
3    a3     b3     c3     a7     b7     c7
4    a4     b4     c4     a8     b8     c8"
DF <- read.table(text = Lines, as.is = TRUE, check.names = FALSE)

DF2 <- setNames(DF, c("A1", "B1", "C1", "A2", "B2", "C2")) # test input


Share : facebook icon twitter icon
Save data.frame objects into .Rds files within a loop

Save data.frame objects into .Rds files within a loop


By : user3376365
Date : March 29 2020, 07:55 AM
I hope this helps you . Use the list= parameter of the save function. This allows you to specify the name of the object as a character vector rather than passing the object itself. For example
code :
lapply(ls(pattern = "errDatas"), function(x) {
    save(list=x, file = paste0(x, ".Rda"))
})
Stack a numeric data frame and change the column names in a single instruction

Stack a numeric data frame and change the column names in a single instruction


By : Alexander
Date : March 29 2020, 07:55 AM
it fixes the issue I have a purely numeric data frame, whose columns I want to stack (concatenate) together. stack does a good job: , You can use the melt function in the reshape2 library:
code :
library(reshape2)
bar <- melt(foo, value.name = "x", variable.name = "y")
How do I stack rows in a Pandas data frame to get one “long row”, preserving column types?

How do I stack rows in a Pandas data frame to get one “long row”, preserving column types?


By : Siddhant Mishra
Date : March 29 2020, 07:55 AM
hope this fix your issue This is a follow up to How do I stack rows in a Pandas data frame to get one "long row"? , You can pivot your table:
code :
# create a unique id for all rows and pivot the table
df['id'] = 0    
df1 = df.reset_index().pivot(index = 'id', columns = 'index')

# collapse multi index columns to single index
df1.columns = ['_'.join(['row'] + [str(c) for c in col][::-1]) for col in df1.columns.values]

df1
#      row_0_numerical  row_1_numerical row_0_stringy   row_1_stringy
# id                
#  0                 2                3             A               B
Take values of a column in a Pandas data frame, add numbers to value and save as new data frame

Take values of a column in a Pandas data frame, add numbers to value and save as new data frame


By : Romario
Date : March 29 2020, 07:55 AM
this one helps. You can do this by first performing a CROSS JOIN (Cartesian Product) on the DataFrame and suffixes.
code :
u = pd.DataFrame(dict(suf=range(20), key=1))
v = df.assign(key=1).merge(u, on='key').drop('key', 1)

v.iloc[:,1] = v.iloc[:,1].astype(str).add(
    v.pop('suf').astype(str).str.zfill(2)).astype(int)
v.head()

     0      1
0  123  23400
1  123  23401
2  123  23402
3  123  23403
4  123  23404
Does R automatically remove '.' from data.frame column names?

Does R automatically remove '.' from data.frame column names?


By : Ashutosh Vaishnav
Date : March 29 2020, 07:55 AM
I hope this helps you . R's $ operator accepts any unambiguous prefix of a column name as referring to that column. It has nothing to do with .s.
For example, try:
Related Posts Related Posts :
  • Statistical mode of a categorical variable in R (using mlv)
  • Using for-loop to mutate a data.frame in r
  • Make plot with regression line for mixed model
  • Shortcut to select matces cases in R studio
  • vectoriced norm/matrix multiplication
  • Negative log10 transformation in R
  • Plot data with duplicate points
  • Visualizing crosstab tables with a plot in R - changing colours
  • How to manually modify automated numbers and labels in plot
  • How can I follow any redirections of a url in R?
  • Add jitter to box plot using markers in plotly
  • Adding an extra item to the legend
  • ggplot fills in data in the wrong order
  • Convert list to data frame
  • R: filtering by list(s) of strings and returning all results that start with the content of the lists
  • R:How to attach parts of a data frame with different headers and/or an overflowing piece of the dat frame
  • How to use 'par' for manipulating plot margins?
  • Can dplyr::case_when return mix of NAs and non-NAs?
  • Text preprocessing and topic modelling using text2vec package
  • Uploading multiple files in Shiny, process the files, rbind the results and return a download
  • R levelplot: color green-white-red (white on 0) according to one variable, but show the values of another variable
  • Why [i] doesn't point to the starting point in a vector
  • In R after generating a mvrnorm distribution, Y, what does Y[,1] do?
  • expand a data frame to have as many rows as range of two columns in original row
  • Getting started with R and CFA
  • Re order x-axis in ggplot so time goes from 12AM to 11PM in R
  • How to format dplyr output in R into doubles (or other workable format)?
  • Dataframe to matrix conversion using tapply turns zeros to NAs
  • Smallest multiple of 1:20 - How can I make it quicker?
  • How to specify the size of a graph in ggplot2 independent of axis labels
  • How can I find the number of a vector's elements in another vector?
  • ROC curve from train/test set in caret R package
  • Random Forest for a mixture of categorical,numeric and "unwanted" variables which include missing values
  • extract certain data from multiple excel files with R
  • Matrix with counts of wins and losses between methods in R
  • Grouping string variables from a dataframe by best string match to make subsets
  • Reorder does not work after adding second geom_points
  • cover POS data formate to the one can apply Arules (Apriori)
  • Matching values between data frames based on overlapping dates
  • Grouped bar chart turns into stacked bar chart ggplot
  • R: How to fill in NA Values within a Column based on grouping?
  • Two action buttons, but only the first one, that is written in the server file, works?
  • Barchart grouped by variable both count up to 100 percent
  • Converting time in R to 24 hours
  • R - Web scrapping and downloading multiple zip files and save the files without overwriting
  • Find month and year inside string
  • Append multiple csv files into one file using R
  • Use `purrr::map` with k-means
  • R - 'data' is not an exported object from 'namespace:my_package'
  • Sum vector with number by dinamic intervals without looping
  • Issues with ave function in R: error "cannot allocate vector of size 419 kb."
  • Shiny system call with continuous updates
  • Unable to un-nest some fields using google bigquery (standard)
  • How to perform row mean in matrix by 3 number of columns in sequence using R
  • Non absolute counts histogram for imbalanced groups
  • Plot_ly color not corresponding to z
  • Numeric calculations using dplyr piping commands
  • Separate a column with uneven/unequal strings and with no delimiters
  • Using lapply and which to subset dataframe by both characteristic and fuction
  • Removing factor levels from variable X based on values in Y
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org