提问人:Raphaella 提问时间:10/14/2023 最后编辑:PhilRaphaella 更新时间:10/16/2023 访问量:152
R 中的列有问题...列不存在
Problem with columns in R... Column does not exist
问:
我是编程语言的新手,我正在学习用于数据分析的 R。我正在参加一项课程活动,我很难重命名“公司......Maker.if.known.“更改为”Maker”。我尝试在我的 RStudio 桌面中输入,
rename(maker = '公司...Maker.if.known.')
它一直说我试图重命名的列没有退出。我已经尝试了 RStudio 可能识别的列名的不同变体,因为即使是“公司......Maker.if.know.'RStudio 无法识别它。我安装了所有软件包并加载了它们,为了更好地衡量,tidyverse、ggplot2 和 dplyr。
我不太确定出了什么问题,或者为什么无法识别该列,我相信其余列也会发生相同的错误。我怎样才能解决这个问题,或者让代码工作?
我将附上一些屏幕截图或我编写的代码以及上下文中的错误消息。
非常感谢您抽出宝贵时间回复!
install.packages("tidyverse")
library(tidyverse)
install.packages("ggplot2")
library(ggplot2)
install.packages("dplyr")
library(dplyr)
flavors_df <- read_csv("flavors_of_cacao.csv")
str(flavors_df)
colnames(flavors_df)
head(flavors_df)
flavors_df %>%
rename(maker = `Company
(Maker-if known)`)
# i also ran the colnames() function, this is what I got
> colnames(flavors_df)
[1] "Company \n(Maker-if known)" "Specific Bean Origin\nor Bar Name" "REF"
[4] "Review\nDate" "Cocoa\nPercent" "Company\nLocation"
[7] "Rating" "Bean\nType" "Broad Bean\nOrigin"
# some of the error messages I got trying to rename
> flavors_df %>%
+ rename(maker = "Company \n(Maker-if known)")
Error in `rename()`:
! Can't rename columns that don't exist.
✖ Column `Company \n(Maker-if known)` doesn't exist.
Run `rlang::last_trace()` to see where the error occurred.
> View(flavors_df)
> flavors_df %>%
+ rename(maker = "Company...Maker.if.know.")
Error in `rename()`:
! Can't rename columns that don't exist.
✖ Column `Company...Maker.if.know.` doesn't exist.
Run `rlang::last_trace()` to see where the error occurred.
> flavors_df %>%
+ rename(maker = "Company (Maker-if known)")
Error in `rename()`:
! Can't rename columns that don't exist.
✖ Column `Company (Maker-if known)` doesn't exist.
Run `rlang::last_trace()` to see where the error occurred.
> flavors_df %>%
+ rename(maker = "Company...Maker.if.known.")
Error in `rename()`:
! Can't rename columns that don't exist.
✖ Column `Company...Maker.if.known.` doesn't exist.
Run `rlang::last_trace()` to see where the error occurred.
> flavors_df %>%
+ rename(maker = Company...Maker.if.known.)
Error in `rename()`:
! Can't rename columns that don't exist.
✖ Column `Company...Maker.if.known.` doesn't exist.
Run `rlang::last_trace()` to see where the error occurred.
> flavors_df %>%
+ rename(maker = Company
+ (Maker-if known))
Error: unexpected symbol in:
" rename(maker = Company
(Maker-if known"
> flavors_df %>%
+ rename(maker = `Company
+ (Maker-if known)`)
Error in `rename()`:
! Can't rename columns that don't exist.
✖ Column `Company \n (Maker-if known)` doesn't exist.
Run `rlang::last_trace()` to see where the error occurred.
我已经尝试了假定列名的不同变体来重命名它,但它一直给出错误消息。我期待重命名“公司...Maker.if.know.“更改为”maker”。
答:
0赞
Derf
10/14/2023
#1
问题来自使用新行语法创建奇怪的列名。read_csv
\n
- 你可以通过改用来修复它。(这修复了
read.csv()
Company \n(Maker-if known)
Company...Maker.if.known.
)
flavors_df <- read.csv("flavors_of_cacao.csv")
flavors_df %>%
rename(maker = `Company...Maker.if.known.`)
- 或者您可以从软件包中使用。(这修复了
clean_names
janitor
Company \n(Maker-if known)
company_maker_if_known
)
flavors_df <- read_csv("flavors_of_cacao.csv")
flavors_df <- janitor::clean_names(flavors_df)
flavors_df %>%
rename(maker = `company_maker_if_known`)
评论
0赞
Raphaella
10/14/2023
你好!非常感谢您的帮助。你的两个解决方案都对我有用。我不认为阅读.csv和read_csv是完全不同的。我会牢记这一点。我也从未想过要做clean_names。我很感激!
0赞
GuedesBF
10/14/2023
#2
我们还可以使用部分匹配来选择其中的列名,如果我们使用 。dplyr
rename_with
flavors_df |>
rename_with(matches("[Mm]aker"), \(x) "maker"))
但是给出的其他解决方案(janitor::clean_names,甚至更好的是,纠正数据导入函数调用upstrem)更具可推广性,并且可能会解决其他问题。
评论
0赞
Raphaella
10/14/2023
你好!非常感谢您的回答。我没想过要用rename_with,我只知道用toupper或tolower来表示这种情况。当我再次尝试我的活动时,我会尝试您的解决方案!我真的很感激!
评论
dput(flavors_df)
colnames()
colnames(flavors_df)[1] <- "maker"