Create a variable in a balanced data frame that is a transpose of another column

proc transpose sas
proc transpose columns to rows
proc transpose wide to long
sas proc transpose multiple variables
dplyr transpose
r spread
how to convert rows into columns in python
proc transpose character variables

I have balanced data set and want to create a new variable based on the transpose of an existing matrix form of another variable. At the moment I am doing this using an intermediate matrix, as such...

library(tidyverse)
set.seed(1)
d <- crossing(orig = LETTERS[1:4], dest = LETTERS[1:4]) %>%
  mutate(x = rnorm(16)) 

m <- matrix(d$x, nrow = 4)

d <- d %>%
  mutate(x_trans = c(t(m)))
d
# # A tibble: 16 x 4
#    orig  dest        x x_trans
#    <chr> <chr>   <dbl>   <dbl>
#  1 A     A     -0.626  -0.626 
#  2 A     B      0.184   0.330 
#  3 A     C     -0.836   0.576 
#  4 A     D      1.60   -0.621 
#  5 B     A      0.330   0.184 
#  6 B     B     -0.820  -0.820 
#  7 B     C      0.487  -0.305 
#  8 B     D      0.738  -2.21  
#  9 C     A      0.576  -0.836 
# 10 C     B     -0.305   0.487 
# 11 C     C      1.51    1.51  
# 12 C     D      0.390   1.12  
# 13 D     A     -0.621   1.60  
# 14 D     B     -2.21    0.738 
# 15 D     C      1.12    0.390 
# 16 D     D     -0.0449 -0.0449

... but i was wondering if there is a way to create such a variable without the matrix/breaking the chian, as I have multiple periods and variables that I want to do this for. At the moment I am using a for loop, but perhaps there is a clever way to this with mutate, some other function and group_by?

I'm not sure what you meant with the loop. But if you is you dont want to break then chain you can simply do like this:

Code

d <- crossing(orig = LETTERS[1:4], dest = LETTERS[1:4]) %>%
  mutate(x = rnorm(16),
         x_trans = x %>% 
           matrix(nrow = 4) %>% 
           t() %>% c())

Result

# A tibble: 16 x 4
   orig  dest        x x_trans
   <chr> <chr>   <dbl>   <dbl>
 1 A     A     -0.626  -0.626 
 2 A     B      0.184   0.330 
 3 A     C     -0.836   0.576 
 4 A     D      1.60   -0.621 
 5 B     A      0.330   0.184 

Manipulating, analyzing and exporting data with tidyverse, Select certain columns in a data frame with the dplyr function select . If we want to create a new object with this smaller version of the data, we can assign it a Spreading and then gathering can be a useful way to balance out a dataset so� Arithmetic operations align on both row and column labels. It can be thought of as a dict-like container for Series objects. This is the primary data structure of the Pandas. Pandas DataFrame.transpose() function transpose index and columns of the dataframe. It reflect the DataFrame over its main diagonal by writing rows as columns and vice-versa.

How about this?

set.seed(1)
d <- crossing(orig = LETTERS[1:4], dest = LETTERS[1:4]) %>%
  mutate(x = rnorm(16)) %>%
  mutate(x_trans = unlist(split(x, dest)))

# # # A tibble: 16 x 4
#   orig  dest        x x_trans
# <chr> <chr>   <dbl>   <dbl>
# 1 A     A     -0.626  -0.626 
# 2 A     B      0.184   0.330 
# 3 A     C     -0.836   0.576 
# 4 A     D      1.60   -0.621 
# 5 B     A      0.330   0.184 
# 6 B     B     -0.820  -0.820 
# 7 B     C      0.487  -0.305 
# 8 B     D      0.738  -2.21  
# 9 C     A      0.576  -0.836 
# 10 C     B     -0.305   0.487 
# 11 C     C      1.51    1.51  
# 12 C     D      0.390   1.12  
# 13 D     A     -0.621   1.60  
# 14 D     B     -2.21    0.738 
# 15 D     C      1.12    0.390 
# 16 D     D     -0.0449 -0.0449

[PDF] 060-2009: Learn the Basics of PROC TRANSPOSE, example, balance amounts for a single customer is found in 4 different, “narrow” rows, The code to create the input datasets is provided in the appendix. Question: do you see how data in rows is transposed to become data in columns ? pandas.DataFrame.transpose¶ DataFrame.transpose (self, * args, copy: bool = False) → ’DataFrame’ [source] ¶ Transpose index and columns. Reflect the DataFrame over its main diagonal by writing rows as columns and vice-versa. The property T is an accessor to the method transpose(). Parameters *args tuple, optional. Accepted for compatibility with NumPy.

There is a pattern in selecting values. We can use any logic to create indices to subset data. Here, I am using outer to generate the indices and then using slice to get data in the particular order.

library(dplyr)
d %>% slice(c(outer(seq(1, n(), 4), 0:3, `+`)))

#   orig  dest     x
#  <chr> <chr>   <dbl>
# 1 A     A     -0.626 
# 2 B     A      0.330 
# 3 C     A      0.576 
# 4 D     A     -0.621 
# 5 A     B      0.184 
# 6 B     B     -0.820 
# 7 C     B     -0.305 
# 8 D     B     -2.21  
# 9 A     C     -0.836 
#10 B     C      0.487 
#11 C     C      1.51  
#12 D     C      1.12  
#13 A     D      1.60  
#14 B     D      0.738 
#15 C     D      0.390 
#16 D     D     -0.0449

Reshaping Data in Python, Whatever column you specify as the columns argument will be used to create new columns (each unique entry will form a new column). Here, the variable has the same 5 variables in both data frames as we have not done any insertion/removal to the variable/column of the data frame. Adding Multiple Observations/Rows To R Data Frame Adding single observations one by one is a repetitive, time-consuming, as well as, a boring task.

group_split then select from d where dest equal to group's first orig

library(dplyr)
library(purrr)
d %>% 
  group_split(orig) %>% 
  #Try `map` if you need the ouptput as a list
  map_df(~.x %>% mutate(x_trans = filter(d, dest==.x$orig[1]) %>% pull(x)))

# A tibble: 16 x 4
   orig  dest        x x_trans
   <chr> <chr>   <dbl>   <dbl>
 1 A     A     -0.626  -0.626 
 2 A     B      0.184   0.330 
 3 A     C     -0.836   0.576 
 4 A     D      1.60   -0.621 
 5 B     A      0.330   0.184 
 6 B     B     -0.820  -0.820 
 7 B     C      0.487  -0.305 
 8 B     D      0.738  -2.21
 ...

Transpose in R, Then I realized there is no such function in dplyr to transpose data. of R packages designed to make data manipulation and exploration cakewalk for R users. function refers to a data frame; The left hand side of the casting function refers to ID variables. The right hand side refers to the variable to move to column name� To create DataFrame from dict of narray/list, all the narray must be of same length. If index is passed then the length index should be equal to the length of arrays. If no index is passed, then by default, index will be range(n) where n is the array length.

Chapter 4 Descriptive statistics and data manipulation, To make things easier for now, we are going to use example data included the data frame, if you want to summarize all variables), a list of grouping variables When you load the data like this, it is a standard data.frame . dplyr functions from the {tibble} package, which adds a new column to the data frame with an id, � A pivoting spec is a data frame that describes the metadata stored in the column name, with one row for each column, and one column for each variable mashed into the column name. The tidyr::pivot_longer_spec() function allows even more specifications on what to do with the data during the transformation.

7.12.4 Attaching a data frame, However, most of the time what you actually want to do is select several different variables within the data frame (i.e., keep only some of the columns), or maybe a � The dummy.data.frame() function creates dummies for all the factors in the data frame supplied. Internally, it uses another dummy() function which creates dummy variables for a single factor.

An Introduction to R, 5.5 The outer product of two arrays; 5.6 Generalized transpose of an array 6.3. 1 Making data frames; 6.3.2 attach() and detach(); 6.3.3 Working with data See Lists. data frames are matrix-like structures, in which the columns can be of different a function for making deposits and a function for stating the current balance. How to Create a Data Frame ; Append a Column to Data Frame ; Select a Column of a Data Frame ; Subset a Data Frame ; How to Create a Data Frame . We can create a data frame by passing the variable a,b,c,d into the data.frame() function. We can name the columns with name() and simply specify the name of the variables. data.frame(df