Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.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 如何根据列的数据提取列名称列表?_R - Fatal编程技术网

R 如何根据列的数据提取列名称列表?

R 如何根据列的数据提取列名称列表?,r,R,我对R很陌生,希望我能说清楚 music <- read.table(text = " AVIS1 AVIS2 AVIS3 AVIS4 AVIS5 1 2 1 2 3 2 2 2 5 2 3 2 3 3 2 5 5 1 4 1 2 5 5 5 5 1 5 1 3 1 6 4 1 4 5 4", header = TRUE) # your data scores <- seq(1, by = -0.5,

我对R很陌生,希望我能说清楚

music <- read.table(text = "
    AVIS1 AVIS2 AVIS3 AVIS4 AVIS5
    1 2 1 2 3 2
    2 2 5 2 3 2
    3 3 2 5 5 1
    4 1 2 5 5 5
    5 1 5 1 3 1
    6 4 1 4 5 4", header = TRUE)              # your data

scores <- seq(1, by = -0.5, length.out = 6)   # vector of scores

library(tidyr)
library(dplyr)

music2 <- music %>%
  gather(AVIS, Value) %>%                     # here you tidy the data
  mutate(score = scores[Value]) %>%           # match score to value
  group_by(AVIS) %>%                          # group AVIS levels
  summarise(score.mean = mean(score)) %>%     # find mean scores for AVIS levels
  arrange(desc(score.mean))                  

list <- list(AVIS = music2$AVIS)              # here is the list

> list$AVIS
[1] "AVIS1" "AVIS5" "AVIS2" "AVIS3" "AVIS4"
我有一个由几个列组成的表,这些列是系数。我想为每一列打分。然后我想计算每个分数的平均值,并显示按其平均分数排序的列列表,这可能吗

music <- read.table(text = "
    AVIS1 AVIS2 AVIS3 AVIS4 AVIS5
    1 2 1 2 3 2
    2 2 5 2 3 2
    3 3 2 5 5 1
    4 1 2 5 5 5
    5 1 5 1 3 1
    6 4 1 4 5 4", header = TRUE)              # your data

scores <- seq(1, by = -0.5, length.out = 6)   # vector of scores

library(tidyr)
library(dplyr)

music2 <- music %>%
  gather(AVIS, Value) %>%                     # here you tidy the data
  mutate(score = scores[Value]) %>%           # match score to value
  group_by(AVIS) %>%                          # group AVIS levels
  summarise(score.mean = mean(score)) %>%     # find mean scores for AVIS levels
  arrange(desc(score.mean))                  

list <- list(AVIS = music2$AVIS)              # here is the list

> list$AVIS
[1] "AVIS1" "AVIS5" "AVIS2" "AVIS3" "AVIS4"
表格将是:
head(musico[,69:73])

music <- read.table(text = "
    AVIS1 AVIS2 AVIS3 AVIS4 AVIS5
    1 2 1 2 3 2
    2 2 5 2 3 2
    3 3 2 5 5 1
    4 1 2 5 5 5
    5 1 5 1 3 1
    6 4 1 4 5 4", header = TRUE)              # your data

scores <- seq(1, by = -0.5, length.out = 6)   # vector of scores

library(tidyr)
library(dplyr)

music2 <- music %>%
  gather(AVIS, Value) %>%                     # here you tidy the data
  mutate(score = scores[Value]) %>%           # match score to value
  group_by(AVIS) %>%                          # group AVIS levels
  summarise(score.mean = mean(score)) %>%     # find mean scores for AVIS levels
  arrange(desc(score.mean))                  

list <- list(AVIS = music2$AVIS)              # here is the list

> list$AVIS
[1] "AVIS1" "AVIS5" "AVIS2" "AVIS3" "AVIS4"
AVIS1 AVIS2 AVIS3 AVIS4 AVIS5
1232
2 2 5 2 3 2
3 3 2 5 1
4 1 2 5 5
515131
641454

music <- read.table(text = "
    AVIS1 AVIS2 AVIS3 AVIS4 AVIS5
    1 2 1 2 3 2
    2 2 5 2 3 2
    3 3 2 5 5 1
    4 1 2 5 5 5
    5 1 5 1 3 1
    6 4 1 4 5 4", header = TRUE)              # your data

scores <- seq(1, by = -0.5, length.out = 6)   # vector of scores

library(tidyr)
library(dplyr)

music2 <- music %>%
  gather(AVIS, Value) %>%                     # here you tidy the data
  mutate(score = scores[Value]) %>%           # match score to value
  group_by(AVIS) %>%                          # group AVIS levels
  summarise(score.mean = mean(score)) %>%     # find mean scores for AVIS levels
  arrange(desc(score.mean))                  

list <- list(AVIS = music2$AVIS)              # here is the list

> list$AVIS
[1] "AVIS1" "AVIS5" "AVIS2" "AVIS3" "AVIS4"
我想为每一项评分:

musico$score1<-0  
musico$score1[musico$AVIS1==1]<-1  
musico$score1[musico$AVIS1==2]<-0.5
music <- read.table(text = "
    AVIS1 AVIS2 AVIS3 AVIS4 AVIS5
    1 2 1 2 3 2
    2 2 5 2 3 2
    3 3 2 5 5 1
    4 1 2 5 5 5
    5 1 5 1 3 1
    6 4 1 4 5 4", header = TRUE)              # your data

scores <- seq(1, by = -0.5, length.out = 6)   # vector of scores

library(tidyr)
library(dplyr)

music2 <- music %>%
  gather(AVIS, Value) %>%                     # here you tidy the data
  mutate(score = scores[Value]) %>%           # match score to value
  group_by(AVIS) %>%                          # group AVIS levels
  summarise(score.mean = mean(score)) %>%     # find mean scores for AVIS levels
  arrange(desc(score.mean))                  

list <- list(AVIS = music2$AVIS)              # here is the list

> list$AVIS
[1] "AVIS1" "AVIS5" "AVIS2" "AVIS3" "AVIS4"

musico$score1这里有一种使用base的方法,尽管您想要什么还不清楚。
score1
AVIS1
有什么关系?我想您可能丢失了
musico
中的一些数据

music <- read.table(text = "
    AVIS1 AVIS2 AVIS3 AVIS4 AVIS5
    1 2 1 2 3 2
    2 2 5 2 3 2
    3 3 2 5 5 1
    4 1 2 5 5 5
    5 1 5 1 3 1
    6 4 1 4 5 4", header = TRUE)              # your data

scores <- seq(1, by = -0.5, length.out = 6)   # vector of scores

library(tidyr)
library(dplyr)

music2 <- music %>%
  gather(AVIS, Value) %>%                     # here you tidy the data
  mutate(score = scores[Value]) %>%           # match score to value
  group_by(AVIS) %>%                          # group AVIS levels
  summarise(score.mean = mean(score)) %>%     # find mean scores for AVIS levels
  arrange(desc(score.mean))                  

list <- list(AVIS = music2$AVIS)              # here is the list

> list$AVIS
[1] "AVIS1" "AVIS5" "AVIS2" "AVIS3" "AVIS4"
根据提供的示例,这里是一个基本的R解决方案
vapply
循环遍历data.frame并生成每列的平均值。然后,
堆栈
顺序
只是为了使输出成为一个看起来不错的数据帧

music <- read.table(text = "
    AVIS1 AVIS2 AVIS3 AVIS4 AVIS5
    1 2 1 2 3 2
    2 2 5 2 3 2
    3 3 2 5 5 1
    4 1 2 5 5 5
    5 1 5 1 3 1
    6 4 1 4 5 4", header = TRUE)              # your data

scores <- seq(1, by = -0.5, length.out = 6)   # vector of scores

library(tidyr)
library(dplyr)

music2 <- music %>%
  gather(AVIS, Value) %>%                     # here you tidy the data
  mutate(score = scores[Value]) %>%           # match score to value
  group_by(AVIS) %>%                          # group AVIS levels
  summarise(score.mean = mean(score)) %>%     # find mean scores for AVIS levels
  arrange(desc(score.mean))                  

list <- list(AVIS = music2$AVIS)              # here is the list

> list$AVIS
[1] "AVIS1" "AVIS5" "AVIS2" "AVIS3" "AVIS4"

music首先引入一个
scores
向量用作查找,我就是这样做的。我假设分数下降了0.5,并且所需分数的数量取决于列中的最大级别数(即
AVIS1
中的6)

music <- read.table(text = "
    AVIS1 AVIS2 AVIS3 AVIS4 AVIS5
    1 2 1 2 3 2
    2 2 5 2 3 2
    3 3 2 5 5 1
    4 1 2 5 5 5
    5 1 5 1 3 1
    6 4 1 4 5 4", header = TRUE)              # your data

scores <- seq(1, by = -0.5, length.out = 6)   # vector of scores

library(tidyr)
library(dplyr)

music2 <- music %>%
  gather(AVIS, Value) %>%                     # here you tidy the data
  mutate(score = scores[Value]) %>%           # match score to value
  group_by(AVIS) %>%                          # group AVIS levels
  summarise(score.mean = mean(score)) %>%     # find mean scores for AVIS levels
  arrange(desc(score.mean))                  

list <- list(AVIS = music2$AVIS)              # here is the list

> list$AVIS
[1] "AVIS1" "AVIS5" "AVIS2" "AVIS3" "AVIS4"
然后,使用
tidyr
可以组织数据集,这样就必须创建包含相应级别的变量(即
AVIS
)。然后使用
dplyr
中的
mutate
函数添加分数变量,其中
score
向量中的分数位置与
value
变量中的值匹配。从这里,您可以找到与
AVIS
级别对应的平均分数,相应地排列它们并将它们放入列表中

music <- read.table(text = "
    AVIS1 AVIS2 AVIS3 AVIS4 AVIS5
    1 2 1 2 3 2
    2 2 5 2 3 2
    3 3 2 5 5 1
    4 1 2 5 5 5
    5 1 5 1 3 1
    6 4 1 4 5 4", header = TRUE)              # your data

scores <- seq(1, by = -0.5, length.out = 6)   # vector of scores

library(tidyr)
library(dplyr)

music2 <- music %>%
  gather(AVIS, Value) %>%                     # here you tidy the data
  mutate(score = scores[Value]) %>%           # match score to value
  group_by(AVIS) %>%                          # group AVIS levels
  summarise(score.mean = mean(score)) %>%     # find mean scores for AVIS levels
  arrange(desc(score.mean))                  

list <- list(AVIS = music2$AVIS)              # here is the list

> list$AVIS
[1] "AVIS1" "AVIS5" "AVIS2" "AVIS3" "AVIS4"
music%#将分数与值匹配
按(AVIS)分组%>%#分组AVIS水平
总结(分数.平均值=平均值(分数))%>%#查找AVIS水平的平均值
排列(描述(分数平均值))
列表$AVIS
[1] “AVIS1”“AVIS5”“AVIS2”“AVIS3”“AVIS4”

非常感谢,堆栈指令正是我想要的!谢谢,我不知道收集说明,非常有用。我最终使用科尔提出的解决方案,因为对我来说似乎更容易。是的,没问题。也许您应该只运行
music2%gather(AVIS,Value)
来查看
gather()
如何操作数据集。这是一个非常简洁的函数。