在保存到csv中之前,将一个单元格数据拆分为两个变量

时间:2019-02-07 00:41:47

标签: r csv dataframe

我正在执行循环中的API Web抓取功能,并将所有绑定保存在一个文件中。整个工作将涉及许多文件的复制。我希望将“观察时间”(第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 :(得分:2)

是的。使用tidy的{​​{1}}将一列拆分为原始列的各个段:

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 timeDate取代。如果需要,可以通过Time将它们转换回整数(或其他形式)。

答案 1 :(得分:1)

要将其保存为日期和时间,首先您需要了解日期和时间变量以什么格式存储(Julian time,roman ...等)。然后,我们需要将它们分成两列,如下所示:

library(tidyr)
separate(data = df, col = 'Observation Time', into = c("Date","Time"), sep = "/")