R中的分组和/或计数

R中的分组和/或计数,r,count,grouping,tidyverse,R,Count,Grouping,Tidyverse,我试图“重新计算”R中的一列,并通过清理数据来解决问题。我正在按位置清理数据,一旦我将CA更改为加利福尼亚州 all_location <- read.csv("all_location.csv", stringsAsFactors = FALSE) all_location <- count(all_location, location) all_location <- all_location[with(all_location, order(-n)), ] a

我试图“重新计算”R中的一列,并通过清理数据来解决问题。我正在按位置清理数据,一旦我将CA更改为加利福尼亚州

 all_location <- read.csv("all_location.csv", stringsAsFactors = FALSE)
 all_location <- count(all_location, location)
 all_location <- all_location[with(all_location, order(-n)), ]

  all_location

   A tibble: 100 x 2
    location        n
   <chr>       <int>
  1 CA           3216
  2 Alaska       2985
 3 Nevada        949
 4 Washington    253
 5 Hawaii        239
 6 Montana       218
 7 Puerto Rico   149
 8 California    126
 9 Utah           83
10 NA             72
  ca1 <- grep("CA",all_location$location)
  all_location$location <- replace(all_location$location,ca1,"California")

 all_location

A tibble: 100 x 2
 location        n
<chr>       <int>
 1 California   3216
 2 Alaska       2985
 3 Nevada        949
 4 Washington    253
 5 Hawaii        239
 6 Montana       218
 7 Puerto Rico   149
 8 California    126
 9 Utah           83
 10 NA             72

all_location
all_location$location[substr(all_location$location,1,5)%in%“California”]因此,在调用
count()
之前,请将CA替换为California…?此外,请检查现有版本的“California”上的单元格填充(额外空白),以确保它不是真正的“California”。这方面做得很好我最终将数据拉回来,然后在运行count命令之前进行了grepped和replace。
all_location$location[substr(all_location$location, 1, 5) %in% "Calif" ] <- "California"