提问人:estrix 提问时间:8/31/2023 更新时间:9/4/2023 访问量:52
防止 R 数据帧中特定行中的小数位
Preventing Decimal Places in Specific Rows in R Data Frame
问:
我正在使用 R 数据帧,其中有表示“年份”和“销售额”的列。我想将这些值显示为没有任何小数位的整数。目前,当我创建表格或打印数据框时,“sales”值显示为小数位(例如,10.00),“year”值也显示小数部分(例如,2019.00)。
我想知道如何设置这些列的格式,以便它们显示为没有任何小数位的整数。我尝试使用 format() 函数,但它似乎不会影响这些特定列。
这是我当前代码的片段:
# Data
sales <- c(10, 30, 50, 20, 40)
revenue <- c(100.25, 300.50, 500.75, 200.00, 400.50)
profit <- c(10.4, 200.7, 400.5, 100.00, 356.79)
year <- c(2019:2023)
data <- rbind(Year = year, Sales = sales, Revenue = revenue, Profit = profit)
# Create a new table with different rows
Table <- kbl(data,
caption = "Development",
booktabs = TRUE,
linesep = c("\\addlinespace[0.2cm]"),
align = c("l", "r")) %>%
kable_styling(latex_options = c("striped"), font_size = 10, position = "left")
Table
有关如何实现“销售额”和“年份”列的这种格式的任何指导将不胜感激。提前感谢您的帮助!
答:
0赞
margusl
8/31/2023
#1
不确定包含的 reprex 如何描述您的实际数据集格式。如果从“年份”/“销售额”/“收入”/“利润”列开始,可以先将所有列格式化为字符串,转置以在字符矩阵中获取每年的列,然后选择性地将其转回 data.frame(也将处理矩阵)。kbl()
library(kableExtra)
library(dplyr, warn.conflicts = FALSE)
# Data
sales <- c(10, 30, 50, 20, 40)
revenue <- c(100.25, 300.50, 500.75, 200.00, 400.50)
profit <- c(10.4, 200.7, 400.5, 100.00, 356.79)
year <- c(2019:2023)
# reprex from Q, a numeric matrix:
data_old <- rbind(Year = year, Sales = sales, Revenue = revenue, Profit = profit)
data_old
#> [,1] [,2] [,3] [,4] [,5]
#> Year 2019.00 2020.0 2021.00 2022 2023.00
#> Sales 10.00 30.0 50.00 20 40.00
#> Revenue 100.25 300.5 500.75 200 400.50
#> Profit 10.40 200.7 400.50 100 356.79
str(data_old)
#> num [1:4, 1:5] 2019 10 100.2 10.4 2020 ...
#> - attr(*, "dimnames")=List of 2
#> ..$ : chr [1:4] "Year" "Sales" "Revenue" "Profit"
#> ..$ : NULL
# a more conventional representation of that dataset:
data_new <- data.frame(Year = year, Sales = sales, Revenue = revenue, Profit = profit)
data_new
#> Year Sales Revenue Profit
#> 1 2019 10 100.25 10.40
#> 2 2020 30 300.50 200.70
#> 3 2021 50 500.75 400.50
#> 4 2022 20 200.00 100.00
#> 5 2023 40 400.50 356.79
str(data_new)
#> 'data.frame': 5 obs. of 4 variables:
#> $ Year : int 2019 2020 2021 2022 2023
#> $ Sales : num 10 30 50 20 40
#> $ Revenue: num 100 300 501 200 400
#> $ Profit : num 10.4 200.7 400.5 100 356.8
data_wide <- data_new %>%
mutate(across(everything(), format)) %>%
tibble::column_to_rownames("Year") %>%
t() %>%
as.data.frame()
data_wide
#> 2019 2020 2021 2022 2023
#> Sales 10 30 50 20 40
#> Revenue 100.25 300.50 500.75 200.00 400.50
#> Profit 10.40 200.70 400.50 100.00 356.79
str(data_wide)
#> 'data.frame': 3 obs. of 5 variables:
#> $ 2019: chr "10" "100.25" " 10.40"
#> $ 2020: chr "30" "300.50" "200.70"
#> $ 2021: chr "50" "500.75" "400.50"
#> $ 2022: chr "20" "200.00" "100.00"
#> $ 2023: chr "40" "400.50" "356.79"
kbl(data_wide,
caption = "Development",
booktabs = TRUE,
linesep = c("\\addlinespace[0.2cm]"),
align = c("l", "r")) %>%
kable_styling(latex_options = c("striped"), font_size = 10, position = "left")
创建于 2023-08-31 使用 reprex v2.0.2
0赞
estrix
9/4/2023
#2
感谢您的帮助;我现在已经使用以下解决方案解决了它。
# Data
sales <- c(10, 30, 50, 20, 40)
revenue <- round(c(100.25, 300.50, 500.75, 200.00, 400.50),1)
profit <- round(c(10.4, 200.7, 400.5, 100.00, 356.79),1)
year <- c(2019:2023)
data <- rbind(Year = year, Sales = sales, Revenue = revenue, Profit = profit)
colnames(data) <- data[1,]; data <- data[-1,]
data <- as.data.frame(data)
data <- data %>%
rowwise()%>%
mutate(across(everything(), ~ if(cur_group_id() >= 2) format(.x, nsmall = 1) else as.character(.x)))
# Create a new table with different rows
Table <- kbl(data,
caption = "Development",
booktabs = TRUE,
linesep = c("\\addlinespace[0.2cm]")) %>%
kable_styling(latex_options = c("striped"), font_size = 10, position = "left")
Table
评论
rbind()
data.frame()
tibble()
data <- data.frame(Year = year, Sales = sales, Revenue = revenue, Profit = profit)
colnames(data) <- data[1,]; data <- data[-1,]
rbind()
data <- do.call(`rbind`, lapply(list(Year = year, Sales = sales, Revenue = revenue, Profit = profit), as.character))