【问题标题】:Split one cell data into two variables before save in csv将一个单元格数据拆分为两个变量,然后保存在 csv 中
【发布时间】:2019-02-07 00:41:47
【问题描述】:

我正在循环函数中执行 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

【问题讨论】:

    标签: r csv dataframe


    【解决方案1】:

    是的。使用tidyseperate 将列拆分为原始列的段:

    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 列被删除并替换为DateTime。如果需要,您可以通过 mutate 将它们转换回整数(或其他内容)。

    【讨论】:

    • 我正在更新我的问题以增加清晰度。我可以直接通过 dat$'Observation time' 而不手动输入观察时间并将其从那里分开吗?
    • 附加我的网络抓取代码可能会吓到其他人,所以我再次编辑以包含一些数据。希望对您有所帮助
    • 应该写在write.csv (dat, 'c/Users/..之前吧?我试过了,但它不起作用..
    • 太多的红线警告让我忽略了新的错误。无论如何它只是输入错误。您建议的代码有效,只是“进入”而不是“到”的问题。我不喜欢简单地复制......谢谢!
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-10
      • 1970-01-01
      • 1970-01-01
      • 2012-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多