Manipulating data with R
3
0
Entering edit mode
19 months ago

Hello,

I have a huge data but I will share just a few.

I would like to manipulate this data using the R language.

My data looks like

  No    Value
    A1  20.0585
    A2  18.2183
    A3  18.965
    A4  15.9794
    B1  16.1047
    B2  18.0323
    B3  19.144
    B4  14.5789
    C1  17.035
    C2  20.2898
    C3  19.4061
    C4  16.0626

and I would like the data to be like

No  1   2   3   4
A   20.0585 18.2183 18.965  15.9794
B   16.1047 18.0323 19.144  14.5789
C   17.035  20.2898 19.4061 16.0626

Can you please help me with that?

Thanks!!!

R • 904 views
ADD COMMENT
3
Entering edit mode
19 months ago
ATpoint 81k
df <- data.frame(No=paste0(rep(LETTERS[1:3], each=4), rep(seq(1,4), 3)),
                 Value=rnorm(12))

library(dplyr)
library(tidyr)

df %>%
  dplyr::mutate(number=sub('.', '', No), letter=substring(No, 1, 1)) %>%
  tidyr::pivot_wider(values_from=Value, names_from=number, id_cols=letter) %>%
  dplyr::rename(No=letter) %>%
  data.frame(., check.names=FALSE)

  No             1          2          3           4
   A -0.5117558617  0.1097707 -0.9361413 -0.05781303
   B  0.0006037038 -1.1819491  1.4818229 -1.14916528
   C -0.6938707580  0.5375405  1.1568835  0.30663738

Next time please provide data via dput() output, not like you do, it makes it hard to copy-paste.

ADD COMMENT
0
Entering edit mode

Thanks so much for your help!!!!!!!

ADD REPLY
2
Entering edit mode
19 months ago

separate comes in handy here.

df |>
  separate(No, c("No", "name"), sep="(?<=^[[:alpha:]])") |>
  pivot_wider(names_from=name, values_from=Value)
ADD COMMENT
1
Entering edit mode
19 months ago
zx8754 11k

Split, then reshape long-to-wide:

library(data.table)

#data
d <- fread("  No    Value
    A1  20.0585
    A2  18.2183
    A3  18.965
    A4  15.9794
    B1  16.1047
    B2  18.0323
    B3  19.144
    B4  14.5789
    C1  17.035
    C2  20.2898
    C3  19.4061
    C4  16.0626")

#split then reshape long-to-wide
d[, c("No", "No1") := tstrsplit(No, "") 
  ][, dcast(.SD, No ~ No1, value.var = "Value") ]

#    No       1       2       3       4
# 1:  A 20.0585 18.2183 18.9650 15.9794
# 2:  B 16.1047 18.0323 19.1440 14.5789
# 3:  C 17.0350 20.2898 19.4061 16.0626
ADD COMMENT

Login before adding your answer.

Traffic: 2524 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6