我喜欢plyr的重命名功能rename.我最近开始使用dplyr,并想知道是否有一种简单的方法可以使用dplyr中的函数重命名变量,这对于plyr来说是否易于使用rename?
我编写了一个函数来使用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)
但没有用.谁能建议我怎么做?
谢谢.
我使用聚合函数来获得因子级别的范围.我正在尝试重命名列,但聚合函数的输出没有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) 我想通过使用变量以动态方式使用 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 中的网络数据(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 中数据集的列名称。例如,将数据集 quine 中的“Eth”列名称更改为“Ethnic”。非常感谢任何帮助或函数名称。
r ×6
dataframe ×2
dplyr ×2
rename ×2
aggregate ×1
data-mining ×1
data-science ×1
ggplot2 ×1
plot ×1