0

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

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This one can work if the elements don't have spaces inside each individual cell.How about if in Column 1 has corporations names, and Column 5 has remarks or comments (phrases) ?
It should also work with spaces in cells as long as the cells are separated by the newline character \n. @user4715 can you provide a working example with what you call remarks or comments?
0

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

1 Comment

This one can work if the elements don't have spaces inside each individual cell.How about if in Column 1 has corporations names, and Column 5 has remarks or comments (phrases) ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.