将 tibble 中的每一行转换为列 header
Convert each row in tibble into column header
我正在尝试将第一列中的所有行都转换为变量。我可以稍后使用 $ 符号(例如 data$SWEAT_index
)调用它们。此时变量名很长,稍后我将通过添加额外的列来简化它。也许我的方法很简单。如何处理这个问题?
indices
# A tibble: 30 x 2
Station Value
<chr> <chr>
1 Station identifier WMKC
2 Station number 48615
3 Observation time 190120/1200
4 Station latitude 6.16
5 Station longitude 102.28
6 Station elevation 5.0
7 Showalter index 1.26
8 Lifted index -2.86
9 LIFT computed using virtual temperature -3.38
10 SWEAT index 187.99
# ... with 20 more rows
data <- indices[-1,]
colnames(data) <-data[,1]
data
# A tibble: 29 x 2
`c("Station number", "Observation time", "Station latitude", "Statio~ `c(48615, NA, 6.16, 102.28~
<chr> <dbl>
1 Station number 48615
2 Observation time NA
3 Station latitude 6.16
4 Station longitude 102.
5 Station elevation 5
6 Showalter index 1.26
7 Lifted index -2.86
8 LIFT computed using virtual temperature -3.38
9 SWEAT index 188.
10 K index 14.4
# ... with 19 more rows
dput(indices)
structure(list(Station = c("Station identifier", "Station number",
"Observation time", "Station latitude", "Station longitude",
"Station elevation", "Showalter index", "Lifted index", "LIFT computed using virtual temperature",
"SWEAT index", "K index", "Cross totals index", "Vertical totals index",
"Totals totals index", "Convective Available Potential Energy",
"CAPE using virtual temperature", "Convective Inhibition", "CINS using virtual temperature",
"Equilibrum Level", "Equilibrum Level using virtual temperature",
"Level of Free Convection", "LFCT using virtual temperature",
"Bulk Richardson Number", "Bulk Richardson Number using CAPV",
"Temp [K] of the Lifted Condensation Level", "Pres [hPa] of the Lifted Condensation Level",
"Mean mixed layer potential temperature", "Mean mixed layer mixing ratio",
"1000 hPa to 500 hPa thickness", "Precipitable water [mm] for entire sounding"
), Value = c(NA, 48615, NA, 6.16, 102.28, 5, 1.26, -2.86, -3.38,
187.99, 14.4, 19, 23.9, 42.9, 409.13, 595.76, -26.9, -8.6, 228.72,
226.79, 819.49, 871.25, 240, 349.48, 294.55, 938.33, 299.97,
17.45, 5782, 46.56)), row.names = c(NA, -30L), class = c("tbl_df",
"tbl", "data.frame"))
正如@NelsonGon 提到的,我们可以使用 spread
new_df <- tidyr::spread(indices, Station, Value)
现在您可以调用单个值,例如 new_df$`Station number
、new_df$`Station identifier
等。
在 base R 中,您可以转置,将其转换为数据框,然后使用 setNames
分配列名
new_df <- setNames(data.frame(t(indices$Value)), indices$Station)
但是,正如@Konrad Rudolph 提到的那样,转置数据帧会弄乱对象的数据类型,因此请小心处理。
我正在尝试将第一列中的所有行都转换为变量。我可以稍后使用 $ 符号(例如 data$SWEAT_index
)调用它们。此时变量名很长,稍后我将通过添加额外的列来简化它。也许我的方法很简单。如何处理这个问题?
indices
# A tibble: 30 x 2
Station Value
<chr> <chr>
1 Station identifier WMKC
2 Station number 48615
3 Observation time 190120/1200
4 Station latitude 6.16
5 Station longitude 102.28
6 Station elevation 5.0
7 Showalter index 1.26
8 Lifted index -2.86
9 LIFT computed using virtual temperature -3.38
10 SWEAT index 187.99
# ... with 20 more rows
data <- indices[-1,]
colnames(data) <-data[,1]
data
# A tibble: 29 x 2
`c("Station number", "Observation time", "Station latitude", "Statio~ `c(48615, NA, 6.16, 102.28~
<chr> <dbl>
1 Station number 48615
2 Observation time NA
3 Station latitude 6.16
4 Station longitude 102.
5 Station elevation 5
6 Showalter index 1.26
7 Lifted index -2.86
8 LIFT computed using virtual temperature -3.38
9 SWEAT index 188.
10 K index 14.4
# ... with 19 more rows
dput(indices)
structure(list(Station = c("Station identifier", "Station number",
"Observation time", "Station latitude", "Station longitude",
"Station elevation", "Showalter index", "Lifted index", "LIFT computed using virtual temperature",
"SWEAT index", "K index", "Cross totals index", "Vertical totals index",
"Totals totals index", "Convective Available Potential Energy",
"CAPE using virtual temperature", "Convective Inhibition", "CINS using virtual temperature",
"Equilibrum Level", "Equilibrum Level using virtual temperature",
"Level of Free Convection", "LFCT using virtual temperature",
"Bulk Richardson Number", "Bulk Richardson Number using CAPV",
"Temp [K] of the Lifted Condensation Level", "Pres [hPa] of the Lifted Condensation Level",
"Mean mixed layer potential temperature", "Mean mixed layer mixing ratio",
"1000 hPa to 500 hPa thickness", "Precipitable water [mm] for entire sounding"
), Value = c(NA, 48615, NA, 6.16, 102.28, 5, 1.26, -2.86, -3.38,
187.99, 14.4, 19, 23.9, 42.9, 409.13, 595.76, -26.9, -8.6, 228.72,
226.79, 819.49, 871.25, 240, 349.48, 294.55, 938.33, 299.97,
17.45, 5782, 46.56)), row.names = c(NA, -30L), class = c("tbl_df",
"tbl", "data.frame"))
正如@NelsonGon 提到的,我们可以使用 spread
new_df <- tidyr::spread(indices, Station, Value)
现在您可以调用单个值,例如 new_df$`Station number
、new_df$`Station identifier
等。
在 base R 中,您可以转置,将其转换为数据框,然后使用 setNames
new_df <- setNames(data.frame(t(indices$Value)), indices$Station)
但是,正如@Konrad Rudolph 提到的那样,转置数据帧会弄乱对象的数据类型,因此请小心处理。