Home > Enterprise >  How to split a 'CHR' column in R?
How to split a 'CHR' column in R?

Time:11-30

I have a dataframe in R (see extract below).

I need to split the 'Weather variable' column into columns for each variable, and although I've tried 'separate', and looked at Tidyverse packages I can't work out the syntax.

Any hints?

          Date         Buffer           LST Weather variable   Value
1   01/12/2010            900   -0.85450387       Wind_trend    0.00
2   01/12/2010            900   -0.85450387      Temperature   11.00
3   01/12/2010            900   -0.85450387   Wind_direction   33.75
4   01/12/2010            900   -0.85450387   Pressure_trend    1.00
5   01/12/2010            900   -0.85450387         Humidity   24.50
6   01/12/2010            900   -0.85450387         Pressure 1024.00
7   01/12/2010            900   -0.85450387       Wind_speed    5.50

CodePudding user response:

Try this way

library(dplyr) 

df%>%  #here yopu should eneter the name of dataset you are using
group_split(Weather variable)

CodePudding user response:

If you want to dummify (or one-hot encode) your variable you can use fastDummies::dummy_cols.

library(fastDummies)
df$WeatherVariable <- as.factor(df$WeatherVariable)
dummyVars(df,"WeatherVariable")

CodePudding user response:

like this?

library(data.table)
setDT(mydata)
dcast(mydata, ... ~ Weather_variable, value.var = "Value")
#          Date Buffer        LST Humidity Pressure Pressure_trend Temperature Wind_direction Wind_speed Wind_trend
# 1: 01/12/2010    900 -0.8545039     24.5     1024              1          11          33.75        5.5          0

sample data used

mydata <- fread("          Date         Buffer           LST Weather_variable   Value
   01/12/2010            900   -0.85450387       Wind_trend    0.00
   01/12/2010            900   -0.85450387      Temperature   11.00
   01/12/2010            900   -0.85450387   Wind_direction   33.75
   01/12/2010            900   -0.85450387   Pressure_trend    1.00
   01/12/2010            900   -0.85450387         Humidity   24.50
   01/12/2010            900   -0.85450387         Pressure 1024.00
   01/12/2010            900   -0.85450387       Wind_speed    5.50")

CodePudding user response:

Reading your question seems you need to split by variable and then pivot_wider() it:

library(tidyr)
library(purrr)

df %>% 
 group_split(Weathervariable) %>%
 map( .f = ~ pivot_wider(.x,names_from = Weathervariable, values_from = Value))

[[1]]
# A tibble: 1 x 4
  Date       Buffer    LST Humidity
  <chr>       <int>  <dbl>    <dbl>
1 01/12/2010    900 -0.855     24.5

[[2]]
# A tibble: 1 x 4
  Date       Buffer    LST Pressure
  <chr>       <int>  <dbl>    <dbl>
1 01/12/2010    900 -0.855     1024

[[3]]
# A tibble: 1 x 4
  Date       Buffer    LST Pressure_trend
  <chr>       <int>  <dbl>          <dbl>
1 01/12/2010    900 -0.855              1
...

Or if you do not need to split:

 df %>%
   pivot_wider(names_from = Weathervariable, values_from = Value)
# A tibble: 1 x 10
  Date       Buffer    LST Wind_trend Temperature Wind_direction Pressure_trend Humidity Pressure Wind_speed
  <chr>       <int>  <dbl>      <dbl>       <dbl>          <dbl>          <dbl>    <dbl>    <dbl>      <dbl>
1 01/12/2010    900 -0.855          0          11           33.8              1     24.5     1024        5.5

With data:

df <-  read.table(text ="
           Date         Buffer           LST Weathervariable   Value
   '01/12/2010'            900   -0.85450387       Wind_trend    0.00
   '01/12/2010'            900   -0.85450387      Temperature   11.00
   '01/12/2010'            900   -0.85450387   Wind_direction   33.75
   '01/12/2010'            900   -0.85450387   Pressure_trend    1.00
   '01/12/2010'            900   -0.85450387         Humidity   24.50
   '01/12/2010'            900   -0.85450387         Pressure 1024.00
   '01/12/2010'            900   -0.85450387       Wind_speed    5.50", header = T)
  •  Tags:  
  • r
  • Related