Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.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中使用map()函数循环时,如何获取变量名而不是x?_R_Dataframe_Map Function - Fatal编程技术网

在R中使用map()函数循环时,如何获取变量名而不是x?

在R中使用map()函数循环时,如何获取变量名而不是x?,r,dataframe,map-function,R,Dataframe,Map Function,我正在对数据集中的数值变量应用用户定义函数,但使用map函数应用时,我得到的不是它们的名称,而是x。如何将map函数中的x替换为variablename 数据集:hd\u trn age sex cp trestbps chol fbs restecg thalach exang <int> <fctr> <fctr> <int> <int> <fctr> <fctr> <in

我正在对数据集中的
数值变量
应用用户定义函数,但使用
map
函数应用时,我得到的不是它们的名称,而是x。如何将
map
函数中的
x
替换为
variable
name

数据集:hd\u trn

age   sex     cp   trestbps chol   fbs   restecg thalach exang
<int> <fctr> <fctr> <int>   <int> <fctr> <fctr>  <int>   <fctr>

63  1   1   145 233 1   2   150 0   
67  1   4   160 286 0   2   108 1   
67  1   4   120 229 0   2   129 1   
37  1   3   130 250 0   0   187 0   
41  0   2   130 204 0   2   172 0   
56  1   2   120 236 0   0   178 0
hd_trn %>% select_if(is.numeric) %>% map(., .f = top_freq_elements)

######### output #########
x      Freq
<fctr> <int>

54  51          
58  43          
55  41          
56  38          
57  38

这可以通过使用例如卷曲
{{{
:=
中的
重命名
来实现,如下所示:

top\u freq\u元素%as.data.frame()%%>%top\n(5,freq)%%>%arrange(desc(freq))
}
图书馆(dplyr)
图书馆(purrr)
hd_trn%>%
如果(是数值)%>%,请选择
imap(功能(特征值、特征名称){
表(特征值)%>%
as.data.frame()%>%#head()
重命名({feature\u name}}:=feature\u value)%>%
顶部(5,频率)%>%
排列(描述(频率))
})
#>$age
#>年龄频率
#> 1  67    2
#> 2  37    1
#> 3  41    1
#> 4  56    1
#> 5  63    1
#> 
#>$sex
#>性频率
#> 1   1    5
#> 2   0    1
#> 
#>$cp
#>cp频率
#> 1  2    2
#> 2  4    2
#> 3  1    1
#> 4  3    1
#> 
#>$trestbps
#>trestbps频率
#> 1      120    2
#> 2      130    2
#> 3      145    1
#> 4      160    1

您可以重命名每个列表中的第一列:

library(dplyr)
library(purrr)

iris %>% 
  select(where(is.numeric)) %>% 
  imap(function(feature_value, feature_name){
    table(feature_value) %>% 
      as.data.frame() %>% 
      rename_with(~feature_name, 1) %>% 
      slice_max(n = 5, Freq) %>% 
      arrange(desc(Freq))
  })
hd_trn %>% 
  select_if(is.numeric) %>% 
  imap(function(feature_value, feature_name){
    table(feature_value) %>% 
      as.data.frame() %>% #head()
      rename(feature_name = feature_value) %>% 
      top_n(5, Freq) %>% 
      arrange(desc(Freq))
  })

#########  output  #########

feature_name Freq
<fctr>       <int>

54  51          
58  43          
55  41          
56  38          
57  38
library(dplyr)
library(purrr)

iris %>% 
  select(where(is.numeric)) %>% 
  imap(function(feature_value, feature_name){
    table(feature_value) %>% 
      as.data.frame() %>% 
      rename_with(~feature_name, 1) %>% 
      slice_max(n = 5, Freq) %>% 
      arrange(desc(Freq))
  })