Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何更改数据帧中变量的名称';s行给定该变量';数据帧中的频率是多少?_R - Fatal编程技术网

R 如何更改数据帧中变量的名称';s行给定该变量';数据帧中的频率是多少?

R 如何更改数据帧中变量的名称';s行给定该变量';数据帧中的频率是多少?,r,R,我在电影的data.frame中有一个变量(分配器,format=factor)。我想替换所有出席“小公司”少于10次的分销商的名称。我能列出一个清单,并使用 aggregate(data.frame(count = distributor), list(value = distributor), length) 但我无法在data.frame中替换 这里有一个使用dplyr的解决方案 library(dplyr) ## make some dummy data df <- tribb

我在电影的data.frame中有一个变量(分配器,format=factor)。我想替换所有出席“小公司”少于10次的分销商的名称。我能列出一个清单,并使用

aggregate(data.frame(count = distributor), list(value = distributor), length)

但我无法在data.frame中替换

这里有一个使用
dplyr
的解决方案

library(dplyr)

## make some dummy data
df <- tribble(
     ~distributor, ~something,
     "dist1", 89,
     "dist2", 92,
     "dist3", 29,
     "dist1", 89
)


df %>% 
     group_by(distributor) %>% 
     ## this counts the number of occurences of each distributor
     mutate(occurrences = n()) %>% 
     ungroup() %>% 
     ## change the name of the distributor if the occurrences are less than 2
     mutate(distributor = ifelse(occurrences < 2, "small company", distributor))
库(dplyr)
##制作一些虚拟数据
df%
分组人(经销商)%>%
##这将统计每个分发服务器的发生次数
变异(出现次数=n())%>%
解组()%>%
##如果出现次数少于2次,请更改分发服务器的名称
变异(分销商=ifelse(出现次数<2,“小公司”,分销商))

如果您包含一个简单的示例输入和所需的输出,可用于测试和验证可能的解决方案,则更容易为您提供帮助。为简单起见,我们假设我们正在尝试更改显示次数少于4次的分销商名称。分销商栏如下所示:Movies$distributors=c(A,A,A,A,B,B,B,c,D)我们希望它看起来像这样:Movies$distributor=c(A,A,A,B,B,B,B,B,B,B,B,B,B,B,B,A,A,A,A,A,A,B,B,B,B,B,B,B,B,B,B,B,B,B,B,A,A,A,A,A,A,A,A,A,A,A。当我们尝试这段代码时,我们不断得到以下错误:mutate_impl(.data,dots)中的错误:无法修改列
distributor
,因为它是一个分组变量。是否包含
ungroup()
行?这将清除分组变量。