[英]How to split a 'CHR' column in R?
我在 R 中有一個 dataframe(見下面的摘錄)。
我需要將“天氣變量”列拆分為每個變量的列,盡管我嘗試了“單獨”,並查看了 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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.