简体   繁体   中英

Transform a list into a dataframe with multiple columns in R

I have a list of names and categories, as follows:

Name
Country
Role
Type
Position
A
USA
P
I
SZ
B
India
R
E
DS
C
USA
H
I
FG
D
France
P
S
RTG
E
USA
R
I
ZA
F
India
H
E
SFG
G
USA
Z
I
DFZ
H
France
Z
S
FSZ

And I would like to put it as follows with R

Column1  Column2   Column3  Column4  Column5
A        USA       P        I        SZ
B        India     R        E        DS
C        USA       H        I        FG
D        France    P        S        RTG
E        USA       R        I        ZA
F        India     H        E        SFG
G        USA       Z        I        DFZ
H        France    Z        S        FSZ

I know how to do it in Excel, using the following formula:

=INDEX($A:$A,ROW(A1)*5-5+COLUMN(A1))

I have no idea how to do it in R.

Thanks

Here is an attempt using tidyverse and assuming your string looks a like:

"Name\nCountry\nRole\nType\nPosition\nA\nUSA\nP\nI\nSZ\nB\nIndia\nR\nE\nDS\nC\nUSA\nH\nI\nFG\nD\nFrance\nP\nS\nRTG\nE\nUSA\nR\nI\nZA\nF\nIndia\nH\nE\nSFG\nG\nUSA\nZ\nI\nDFZ\nH\nFrance\nZ\nS\nFSZ"

in the R Console.

library(tidyverse)
mystring <- "Name\nCountry\nRole\nType\nPosition\nA\nUSA\nP\nI\nSZ\nB\nIndia\nR\nE\nDS\nC\nUSA\nH\nI\nFG\nD\nFrance\nP\nS\nRTG\nE\nUSA\nR\nI\nZA\nF\nIndia\nH\nE\nSFG\nG\nUSA\nZ\nI\nDFZ\nH\nFrance\nZ\nS\nFSZ" 
dd <- mystring %>% 
  str_split("\\n") %>%
  first() %>% 
  matrix(ncol = 5, byrow = TRUE)

It remains to extract the first line and use colnames(dd) <- to assign them to your columnames, see ?colnames .

Base R:

# data    
s <- "Name\nCountry\nRole\nType\nPosition\nA\nUSA space\nP space\nI\nSZ\nB\nIndia\nR\nE\nDS\nC\nUSA\nH\nI\nFG\nD\nFrance\nP\nS\nRTG\nE\nUSA\nR\nI\nZA\nF\nIndia\nH\nE\nSFG\nG\nUSA\nZ\nI\nDFZ\nH\nFrance\nZ\nS\nFSZ"

n_col <- 5
df <- read.table(text = s, sep="\n")
n_row <- nrow(df)/n_col

df <- as.data.frame(matrix(df$V1, n_row, n_col, byrow=TRUE))[-1,]

Output

V1     V2 V3 V4  V5
1  A    USA  P  I  SZ
2  B  India  R  E  DS
3  C    USA  H  I  FG
4  D France  P  S RTG
5  E    USA  R  I  ZA
6  F  India  H  E SFG
7  G    USA  Z  I DFZ
8  H France  Z  S FSZ

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM