R Reshape a Vector into Multiple Columns

R reshape a vector into multiple columns

You can do

dim(d) <- c(10, 10)
d <- t(d)

or

d <- matrix(d, nrow = 10, byrow = TRUE)

Convert a vector to data frame with multiple columns

You can do like this: split your vector according to a vector of repeated 1:7, then apply cbind.data.frame. Eventually, add column names

x=c("#99", "Hershey", "$6.7 B", "7%", "$4.7 B", "$562 M", "Consumer Packaged Goods", "#100", "Costco", "$6.7 B", "14%", "$117.3 B",  "-",  "Retail")
res <- cbind.data.frame(split(x, rep(1:7, times=length(x)/7)), stringsAsFactors=F)
names(res) <- c("S.NO", "Brand", "Brandval", "Change", "Revenue", "Cost", "Industry")
str(res)
#### 'data.frame': 2 obs. of 7 variables:
#### $ S.NO : chr "#99" "#100"
#### $ Brand : chr "Hershey" "Costco"
#### ...

You can choose the option StringAsFactors so that you get either character or factor columns

R Split a column containing a vector into multiple columns

Here's one solution using dplyr and tidyr:

library(dplyr)
library(tidyr)

df %>%
unnest(a) %>%
group_by(id) %>%
mutate(key = row_number()) %>%
spread(key, a)

# A tibble: 2 x 4
# Groups: id [2]
id `1` `2` `3`
* <dbl> <chr> <chr> <chr>
1 1.00 aa bb cc
2 2.00 aa bb cc

Using data, provided by Florian:

df <- data.frame(
id = c(1,2),
a = I(list(c("aa", "bb", "cc"), c("aa", "bb", "cc")))
)

Reshaping matrix into vector of alternate columns

We can transpose the matrix and convert it to a vector

c(t(mat1))

converting a vector into a dataframe columnwise

You can transpose the vector and convert it into dataframe/tibble.

t(x) %>% as_tibble()
t(x) %>% data.frame()

# estimate ci.low ci.up
#1 0.595 0.11 2.004

Reshaping a vector from long to wide

I would create an interactive table in markdown with the DT package. Link to vignette

library(DT)

datatable(
dataframe, class = 'cell-border stripe', extensions = c('Buttons', 'FixedColumns'), options = list(
dom = 'Bfrtip', scrollX = TRUE, fixedColumns = TRUE,
buttons = c('copy', 'csv', 'excel', 'pdf', 'print')
)
)

Explore the vignette, it has a bunch of options such as: formating fields with colors and shapes, enabling user to add or remove columns interactivly, scorling through wide tables, ect.

How do I reshape a character vector based on the contents in R?

Here is one way in base R.

Based on some pattern in Participant name we can find their position using grep. In the example shared the pattern is every Participant has an upper-case letter. We use their position to split data so each Participant has their own list. We use the first value in each list as Participant name and alternate values as Time.point and Score respectively.

output <- do.call(rbind, lapply(split(l, 
findInterval(seq_along(l), grep('[A-Z]', l))), function(x) {
data.frame(Participant = x[1],
Time.Point = x[-1][c(TRUE, FALSE)],
Score = x[-1][c(FALSE, TRUE)])
}))
rownames(output) <- NULL
output <- type.convert(output)
output

# Participant Time.Point Score
#1 A 1 27
#2 B 1 26
#3 B 2 54


Related Topics



Leave a reply



Submit