Reputation: 55
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
Upvotes: 0
Views: 54
Reputation: 163
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
.
Upvotes: 1
Reputation: 4233
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
Upvotes: 0