相关疑难解决方法(0)

在dplyr中替换"重命名"

我喜欢plyr的重命名功能rename.我最近开始使用dplyr,并想知道是否有一种简单的方法可以使用dplyr中的函数重命名变量,这对于plyr来说是否易于使用rename

r rename dplyr

110
推荐指数
4
解决办法
15万
查看次数

R:ggplot指定列的索引而不是名称

我编写了一个函数来使用ggplot函数获得比例堆积条形图.现在我在这里使用列名ID.

PropBarPlot<-function(df, mytitle=""){
    melteddf<-melt(df, id="ID", na.rm=T)
    ggplot(melteddf, aes(ID, value, fill=variable)) + 
      geom_bar(position="fill") + 
      theme(axis.text.x = element_text(angle=90, vjust=1)) + 
      labs(title=mytitle)
}
Run Code Online (Sandbox Code Playgroud)

我想让它变得通用.所以我想使用列索引而不是列名.我尝试过做这样的事情.

PropBarPlot<-function(df, mytitle=""){
    melteddf<-melt(df, id=names(df)[1], na.rm=T)
    ggplot(melteddf, aes(names(df)[1], value, fill=variable)) + 
      geom_bar(position="fill") + 
      theme(axis.text.x = element_text(angle=90, vjust=1)) + 
      labs(title=mytitle)
}
Run Code Online (Sandbox Code Playgroud)

但没有用.谁能建议我怎么做?

谢谢.

plot r ggplot2 dataframe

7
推荐指数
1
解决办法
4741
查看次数

聚合打印不正确的列数

我使用聚合函数来获得因子级别的范围.我正在尝试重命名列,但聚合函数的输出没有min和max作为单独的列.

# example data
size_cor <- data.frame(SpCode = rep(c(200, 400, 401), 3),
                       Length = c(45, 23, 56, 89, 52, 85, 56, 45, 78))

# aggregate function
spcode_range <- with(size_cor, aggregate(Length, list(SpCode), FUN = range))
Run Code Online (Sandbox Code Playgroud)

输出:

spcode_range 

  Group.1 x.1 x.2
1     200  45  89
2     400  23  52
3     401  56  85
Run Code Online (Sandbox Code Playgroud)

数据结构:

str(spcode_range)

'data.frame':   3 obs. of  2 variables:
 $ Group.1: num  200 400 401
 $ x      : num [1:3, 1:2] 45 23 56 89 52 85

dim(spcode_range)
[1] 3 …
Run Code Online (Sandbox Code Playgroud)

aggregate r dataframe

5
推荐指数
1
解决办法
2512
查看次数

如何使用字符对象通过 dplyr 重命名列

我想通过使用变量以动态方式使用 dplyr 重命名列。但是,它只是为列命名变量的名称,而不是其内容。有任何想法吗??

colnames(y)
[1] "time"         "channel_1"    "channel_2"    "channel_3"    "channel_4"    "channel_5"    "correction"   "channel.corr"

ladder.channel <- "channel_4"
fsa.bc <- y %>%
    select(-c(all_of(ladder.channel), "correction")) %>%
    rename(ladder.channel = channel.corr)

colnames(fsa.bc)
"time"           "channel_1"      "channel_2"      "channel_3"      "channel_5"      "ladder.channel"
Run Code Online (Sandbox Code Playgroud)

r rename dplyr

5
推荐指数
1
解决办法
2484
查看次数

在 R 读取的数据文件中分配列名

我正在尝试读取 R 中的网络数据(id 图形)。该文件名为“network.txt”,数据如下:

4 0
5 0
6 0
7 0
8 0
9 0
4029 1
4030 1
4031 1
4032 1
4033 1
19088 9040
19089 9040
19090 9040
19091 9040
19092 9040
19093 9040
19094 9040
19095 9040
19096 9040
19097 9040
Run Code Online (Sandbox Code Playgroud)

而且,我正在使用 read.table() 模块阅读它。

data = read.table("network.txt",sep="\t",header=FALSE)
colnames( data ) <- unlist(c('to', 'from'))

Error in `colnames<-`(`*tmp*`, value = c("to", "from")) : 
  'names' attribute [2] must be the same length as the vector [1]
Run Code Online (Sandbox Code Playgroud)

那么,如何分配列名呢?读取原始数据文件有没有错误?

r data-mining data-science

4
推荐指数
1
解决办法
9168
查看次数

更改 R 中数据集的列名称

我是一个完全的新手,正在尝试更改 R 中数据集的列名称。例如,将数据集 quine 中的“Eth”列名称更改为“Ethnic”。非常感谢任何帮助或函数名称。

r

2
推荐指数
1
解决办法
2万
查看次数

标签 统计

r ×6

dataframe ×2

dplyr ×2

rename ×2

aggregate ×1

data-mining ×1

data-science ×1

ggplot2 ×1

plot ×1