Or if your data is really large, you can try data.table::dcast().
> library(data.table)
> dcast(ID ~ station, data = as.data.table(df1))
ID xy xz
1: 12 15 20
2: 13 16 19
(Note: instead of `as.data.table()`, you can use `setDT` or create your
object as a data.table in the first place.)
On 10/13/22 11:22 PM, Rui Barradas wrote:> Hello,
>
> To reshape from long to wide format, here are two options:
>
>
> df1 <- 'ID??? station? value
> 12????? xy??????? 15
> 12????? xz??????? 20
> 13?????? xy?????? 16
> 13?????? xz?????? 19'
> df1 <- read.table(textConnection(df1), header = TRUE)
>
>
> # base R
> reshape(df1, direction = "wide", idvar = "ID", timevar
= "station")
> #>?? ID value.xy value.xz
> #> 1 12?????? 15?????? 20
> #> 3 13?????? 16?????? 19
>
> # tidyverse
> tidyr::pivot_wider(df1, ID, names_from = station)
> #> # A tibble: 2 ? 3
> #>????? ID??? xy??? xz
> #>?? <int> <int> <int>
> #> 1??? 12??? 15??? 20
> #> 2??? 13??? 16??? 19
>
>
> This question is StackOverflow question [1].
>
> [1]
>
https://stackoverflow.com/questions/5890584/how-to-reshape-data-from-long-to-wide-format
>
>
>
> Hope this helps,
>
> Rui Barradas
>
>
> ?s 19:08 de 13/10/2022, G?bor Malomsoki escreveu:
>> Dears,
>>
>> i need to create from a column of observations variables in a datafram
>> like
>> this way:
>> example:
>> original:
>> ID??? station? value
>> 12????? xy??????? 15
>> 12????? xz??????? 20
>> 13?????? xy?????? 16
>> 13?????? xz?????? 19
>>
>> new df:
>>
>> ? ID????? xy???? xz
>> 12????? 15???? 20
>> 13????? 16???? 19
>>
>> i have been looking around for examples, but i could not find any how
to
>> change my df.
>> I would like to make regression analysis on the values from different
>> production stations, so my df is very huge.
>>
>> Please help on finding the package, description or anything else could
>> help.
>>
>> Thank you in advance!
>>
>> Best regards
>> Malo
>>
>> ????[[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>