如何在 R 中拆分 'CHR' 列?

How to split a 'CHR' column in R?

我在 R 中有一个数据框(请参阅下面的摘录)。

我需要将 'Weather variable' 列拆分为每个变量的列,尽管我已经尝试 'separate',并查看了 Tidyverse 包,但我无法理解语法。

有什么提示吗?

          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

试试这个方法

library(dplyr) 

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

如果您想虚拟化(或一次性编码)您的变量,您可以使用 fastDummies::dummy_cols

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

像这样?

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

使用的示例数据

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")

阅读您的问题似乎您需要按变量拆分,然后 pivot_wider() 它:

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
...

或者如果你不需要分割:

 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

有数据:

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)