Разделите данные одной ячейки на две переменные перед сохранением в CSV - PullRequest
0 голосов
/ 07 февраля 2019

Я делаю очистку веб-интерфейса API в функции цикла и сохраняю все привязки в одном файле.Вся работа будет включать в себя воспроизведение многих файлов.Я хочу разделить время наблюдения (19-й столбец) на дату и час, а также хочу, чтобы дальнейшие работы по чтению и графике могли распознавать столбец даты в формате даты.Можно ли сделать это перед сохранением CSV или мне нужно открыть файл CSV, чтобы выполнить это?

startDate <- as.Date("01-11-14", format="%d-%m-%y")
endDate <- as.Date("31-01-15",format="%d-%m-%y")

days <- seq(startDate, endDate, "1 day")

lapply(days[1:92], function(day) {
  get_sounding_data(
region = "seasia",
date = day,
from_hr = "00",
to_hr = "00",
station_number = "48615")
}) -> soundings_48615

new_df <- map(soundings_48615, . %>% mutate_all(parse_guess))
str(new_df)

library(tidyr)
library(dplyr)
library(xlsx)
dat <- bind_rows(new_df)

write.csv(dat, 'c:/Users/Hp/Documents/yr/climatology/yr_SoundingIndexLowerPart/2014.csv')
get_sounding_data <- NULL

здесь, как это выглядит в csv ..

    ... mixing ratio    temperature  Observation time   Precip.
  1         18.48           299.23        141102/0000    64.52
  2         17.28           298.81        141103/0000    59.4
  3         18.65           299.07        141104/0000    61.5

здесьэто данные

str(new_df)
List of 8
 $ :'data.frame':   0 obs. of  0 variables
 $ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   1 obs. of  30 variables:
  ..$ 1000 hPa to 500 hPa thickness              : num 5775
  ..$ Bulk Richardson Number                     : num 3003
  ..$ Bulk Richardson Number using CAPV          : num 3325
  ..$ CAPE using virtual temperature             : num 1103
  ..$ CINS using virtual temperature             : num -22.1
  ..$ Convective Available Potential Energy      : num 996
  ..$ Convective Inhibition                      : num -31.3
  ..$ Cross totals index                         : num 20.9
  ..$ Equilibrum Level                           : num 151
  ..$ Equilibrum Level using virtual temperature : num 151
  ..$ K index                                    : num 38.7
  ..$ Level of Free Convection                   : num 822
  ..$ LFCT using virtual temperature             : num 833
  ..$ LIFT computed using virtual temperature    : num -3.05
  ..$ Lifted index                               : num -2.59
  ..$ Mean mixed layer mixing ratio              : num 18.5
  ..$ Mean mixed layer potential temperature     : num 299
  ..$ Observation time                           : chr "141102/0000"
  ..$ Precipitable water [mm] for entire sounding: num 64.5
  ..$ Pres [hPa] of the Lifted Condensation Level: num 961
  ..$ Showalter index                            : num -0.9
  ..$ Station elevation                          : num 5
  ..$ Station identifier                         : chr "WMKC"
  ..$ Station latitude                           : num 6.16
  ..$ Station longitude                          : num 102
  ..$ Station number                             : int 48615
  ..$ SWEAT index                                : num 211
  ..$ Temp [K] of the Lifted Condensation Level  : num 296
  ..$ Totals totals index                        : num 44.6
  ..$ Vertical totals index                      : num 23.7
 $ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   1 obs. of  30 variables:
  ..$ 1000 hPa to 500 hPa thickness              : num 5771
  ..$ Bulk Richardson Number                     : num 328
  ..$ Bulk Richardson Number using CAPV          : num 395
  ..$ CAPE using virtual temperature             : num 502
  ..$ CINS using virtual temperature             : num -55.3
  ..$ Convective Available Potential Energy      : num 417
  ..$ Convective Inhibition                      : num -61.7
  ..$ Cross totals index                         : num 21.1
  ..$ Equilibrum Level                           : num 167
  ..$ Equilibrum Level using virtual temperature : num 167
  ..$ K index                                    : num 35.1
  ..$ Level of Free Convection                   : num 826
  ..$ LFCT using virtual temperature             : num 835
  ..$ LIFT computed using virtual temperature    : num -2.38
  ..$ Lifted index                               : num -1.98
  ..$ Mean mixed layer mixing ratio              : num 17.3
  ..$ Mean mixed layer potential temperature     : num 299
  ..$ Observation time                           : chr "141103/0000"
  ..$ Precipitable water [mm] for entire sounding: num 59.4
  ..$ Pres [hPa] of the Lifted Condensation Level: num 952
  ..$ Showalter index                            : num -0.65
  ..$ Station elevation                          : num 5
  ..$ Station identifier                         : chr "WMKC"
  ..$ Station latitude                           : num 6.16
  ..$ Station longitude                          : num 102
  ..$ Station number                             : int 48615
  ..$ SWEAT index                                : num 212
  ..$ Temp [K] of the Lifted Condensation Level  : num 295
  ..$ Totals totals index                        : num 44.6
  ..$ Vertical totals index                      : num 23.5
 $ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   1 obs. of  30 variables:
  ..$ 1000 hPa to 500 hPa thickness              : num 5775
  ..$ Bulk Richardson Number                     : num 431
  ..$ Bulk Richardson Number using CAPV          : num 480
  ..$ CAPE using virtual temperature             : num 1354
  ..$ CINS using virtual temperature             : num -8.6
  ..$ Convective Available Potential Energy      : num 1217
  ..$ Convective Inhibition                      : num -11.3
  ..$ Cross totals index                         : num 21.7
  ..$ Equilibrum Level                           : num 142
  ..$ Equilibrum Level using virtual temperature : num 142
  ..$ K index                                    : num 37.4
  ..$ Level of Free Convection                   : num 888
  ..$ LFCT using virtual temperature             : num 896
  ..$ LIFT computed using virtual temperature    : num -3.67
  ..$ Lifted index                               : num -3.3
  ..$ Mean mixed layer mixing ratio              : num 18.6
  ..$ Mean mixed layer potential temperature     : num 299
  ..$ Observation time                           : chr "141104/0000"
  ..$ Precipitable water [mm] for entire sounding: num 61.5
  ..$ Pres [hPa] of the Lifted Condensation Level: num 966
  ..$ Showalter index                            : num -0.97
  ..$ Station elevation                          : num 5
  ..$ Station identifier                         : chr "WMKC"
  ..$ Station latitude                           : num 6.16
  ..$ Station longitude                          : num 102
  ..$ Station number                             : int 48615
  ..$ SWEAT index                                : num 226
  ..$ Temp [K] of the Lifted Condensation Level  : num 296
  ..$ Totals totals index                        : num 44.4
  ..$ Vertical totals index                      : num 22.7
 $ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   1 obs. of  30 variables:
  ..$ 1000 hPa to 500 hPa thickness              : num 5771
  ..$ Bulk Richardson Number                     : num 215
  ..$ Bulk Richardson Number using CAPV          : num 236
  ..$ CAPE using virtual temperature             : num 910
  ..$ CINS using virtual temperature             : num -37.2
  ..$ Convective Available Potential Energy      : num 829
  ..$ Convective Inhibition                      : num -43.9
  ..$ Cross totals index                         : num 21.9
  ..$ Equilibrum Level                           : num 168
  ..$ Equilibrum Level using virtual temperature : num 167
  ..$ K index                                    : num 39.7
  ..$ Level of Free Convection                   : num 829
  ..$ LFCT using virtual temperature             : num 842
  ..$ LIFT computed using virtual temperature    : num -2.68
  ..$ Lifted index                               : num -2.43
  ..$ Mean mixed layer mixing ratio              : num 18
  ..$ Mean mixed layer potential temperature     : num 299
  ..$ Observation time                           : chr "141105/0000"
  ..$ Precipitable water [mm] for entire sounding: num 64.9
  ..$ Pres [hPa] of the Lifted Condensation Level: num 963
  ..$ Showalter index                            : num -1.45
  ..$ Station elevation                          : num 5
  ..$ Station identifier                         : chr "WMKC"
  ..$ Station latitude                           : num 6.16
  ..$ Station longitude                          : num 102
  ..$ Station number                             : int 48615
  ..$ SWEAT index                                : num 229
  ..$ Temp [K] of the Lifted Condensation Level  : num 295
  ..$ Totals totals index                        : num 45.2
  ..$ Vertical totals index                      : num 23.3
 $ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   1 obs. of  30 variables:
  ..$ 1000 hPa to 500 hPa thickness              : num 5757
  ..$ Bulk Richardson Number                     : num 59
  ..$ Bulk Richardson Number using CAPV          : num 70.8
  ..$ CAPE using virtual temperature             : num 546
  ..$ CINS using virtual temperature             : num -51.5
  ..$ Convective Available Potential Energy      : num 455
  ..$ Convective Inhibition                      : num -64.1
  ..$ Cross totals index                         : num 20
  ..$ Equilibrum Level                           : num 194
  ..$ Equilibrum Level using virtual temperature : num 193
  ..$ K index                                    : num 35.3
  ..$ Level of Free Convection                   : num 808
  ..$ LFCT using virtual temperature             : num 834
  ..$ LIFT computed using virtual temperature    : num -2.24
  ..$ Lifted index                               : num -2.04
  ..$ Mean mixed layer mixing ratio              : num 17.3
  ..$ Mean mixed layer potential temperature     : num 299
  ..$ Observation time                           : chr "141106/0000"
  ..$ Precipitable water [mm] for entire sounding: num 59.6
  ..$ Pres [hPa] of the Lifted Condensation Level: num 949
  ..$ Showalter index                            : num 0.42
  ..$ Station elevation                          : num 5
  ..$ Station identifier                         : chr "WMKC"
  ..$ Station latitude                           : num 6.16
  ..$ Station longitude                          : num 102
  ..$ Station number                             : int 48615
  ..$ SWEAT index                                : num 201
  ..$ Temp [K] of the Lifted Condensation Level  : num 295
  ..$ Totals totals index                        : num 43.5
  ..$ Vertical totals index                      : num 23.5
 $ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   1 obs. of  30 variables:
  ..$ 1000 hPa to 500 hPa thickness              : num 5777
  ..$ Bulk Richardson Number                     : num 80.3
  ..$ Bulk Richardson Number using CAPV          : num 86.5
  ..$ CAPE using virtual temperature             : num 1149
  ..$ CINS using virtual temperature             : num -9.57
  ..$ Convective Available Potential Energy      : num 1067
  ..$ Convective Inhibition                      : num -12.4
  ..$ Cross totals index                         : num 21.6
  ..$ Equilibrum Level                           : num 143
  ..$ Equilibrum Level using virtual temperature : num 142
  ..$ K index                                    : num 38.6
  ..$ Level of Free Convection                   : num 845
  ..$ LFCT using virtual temperature             : num 901
  ..$ LIFT computed using virtual temperature    : num -3.03
  ..$ Lifted index                               : num -2.79
  ..$ Mean mixed layer mixing ratio              : num 18.3
  ..$ Mean mixed layer potential temperature     : num 299
  ..$ Observation time                           : chr "141107/0000"
  ..$ Precipitable water [mm] for entire sounding: num 66
  ..$ Pres [hPa] of the Lifted Condensation Level: num 965
  ..$ Showalter index                            : num -1.15
  ..$ Station elevation                          : num 5
  ..$ Station identifier                         : chr "WMKC"
  ..$ Station latitude                           : num 6.16
  ..$ Station longitude                          : num 102
  ..$ Station number                             : int 48615
  ..$ SWEAT index                                : num 239
  ..$ Temp [K] of the Lifted Condensation Level  : num 296
  ..$ Totals totals index                        : num 44.9
  ..$ Vertical totals index                      : num 23.3
 $ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   1 obs. of  30 variables:
  ..$ 1000 hPa to 500 hPa thickness              : num 5775
  ..$ Bulk Richardson Number                     : num 29.1
  ..$ Bulk Richardson Number using CAPV          : num 32.7
  ..$ CAPE using virtual temperature             : num 503
  ..$ CINS using virtual temperature             : num -5.45
  ..$ Convective Available Potential Energy      : num 447
  ..$ Convective Inhibition                      : num -8.42
  ..$ Cross totals index                         : num 20.8
  ..$ Equilibrum Level                           : num 197
  ..$ Equilibrum Level using virtual temperature : num 197
  ..$ K index                                    : num 35.7
  ..$ Level of Free Convection                   : num 871
  ..$ LFCT using virtual temperature             : num 875
  ..$ LIFT computed using virtual temperature    : num -1.07
  ..$ Lifted index                               : num -0.9
  ..$ Mean mixed layer mixing ratio              : num 17.7
  ..$ Mean mixed layer potential temperature     : num 298
  ..$ Observation time                           : chr "141108/0000"
  ..$ Precipitable water [mm] for entire sounding: num 65.8
  ..$ Pres [hPa] of the Lifted Condensation Level: num 966
  ..$ Showalter index                            : num 0.29
  ..$ Station elevation                          : num 5
  ..$ Station identifier                         : chr "WMKC"
  ..$ Station latitude                           : num 6.16
  ..$ Station longitude                          : num 102
  ..$ Station number                             : int 48615
  ..$ SWEAT index                                : num 242
  ..$ Temp [K] of the Lifted Condensation Level  : num 295
  ..$ Totals totals index                        : num 41.7
  ..$ Vertical totals index                      : num 20.9

Ответы [ 2 ]

0 голосов
/ 07 февраля 2019

, чтобы сохранить его в дату и время, сначала вам нужно понять, в каком формате хранятся переменные даты и времени (юлианское время, римское ... и т. Д.).Затем нам нужно разделить их на 2 столбца с чем-то вроде:

library(tidyr)
separate(data = df, col = 'Observation Time', into = c("Date","Time"), sep = "/")
0 голосов
/ 07 февраля 2019

Да.Используйте tidy seperate, чтобы разбить столбец на сегменты исходного:

library(tidyverse)

df <- tibble(
  `Observation time` = c(
    '141102/0000',
    '141103/0000',
    '141104/0000'
  )
)

df %>% separate(
  col = `Observation time`,
  into = c('Date', 'Time'),
  sep = '/'
)

Это приводит к:

# A tibble: 3 x 2
  Date   Time 
  <chr>  <chr>
1 141102 0000 
2 141103 0000 
3 141104 0000 

Обратите внимание, что столбец Observation time удалени заменены на Date и Time.Если вы хотите, вы можете преобразовать их обратно в целое число (или что-то еще) через mutate.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...