My original data frame diasyhoras
has 3 columns:
我最初的数据框架一相纸有3列:
a) "Dia", "Visitas", "Hora"
一)“印度”、“Visitas”、“赫拉”
I need to take the "Dia" column and put it's values as rownames.
我需要使用“Dia”列并将其值作为行名。
str(diasyhoras)
'data.frame': 175 obs. of 3 variables:
$ Dia : Factor w/ 7 levels "Domingo","Jueves",..: 1 3 4 5 2 7 6 1 3 4 ...
$ Visitas: num 271 493 787 853 285 712 782 16 157 734 ...
$ Hora : int 0 0 0 0 0 0 0 1 1 1 ...
The end goals was to use the new df(only numeric values) to plot a heatmap, using the d3heatmap
library from Rstudio (I did not find a single tutorial on this package, so i'm doing my best).
最终的目标是使用新的df(只有数字值)绘制一个heatmap,使用Rstudio的d3heatmap库(我没有在这个包中找到任何教程,所以我正在尽我最大的努力)。
So the help from d3heatmap
says that the first argument should be a "A numeric matrix Defaults to TRUE unless x contains any NAs."
因此,d3heatmap的帮助表明,第一个参数应该是“一个数字矩阵默认为TRUE,除非x包含任何NAs”。
I've tried this:
我已经试过这个:
1. diasyhoras2 <- diasyhoras[,-1] #Removes the "Dia" column and creates a new df.
2. rownames(diasyhoras2) <- diasyhoras[,1]
However, step 2 gives me this error, because i do have duplicated values in my "Dia" column.
然而,第2步给出了这个错误,因为我在“Dia”列中有重复的值。
Error in `row.names<-.data.frame`(`*tmp*`, value = value) :
duplicate 'row.names' are not allowed
In addition: Warning message:
non-unique values when setting 'row.names': ‘Domingo’, ‘Jueves’, ‘Lunes’, ‘Martes’, ‘Miércoles’, ‘Sábado’, ‘Viernes’
UPDATE 1:
更新1:
This is not possible and it was not necessary. What i needed to do is transform the data frame from "long" to "wide" to feed my heatmap (with reshape2
). It was a nice exercise to try to do it using base R. Thanks to all.
这是不可能的,也没有必要。我需要做的是将数据帧从“long”转换为“wide”,以满足我的热图(与reshape2)。这是一个很好的练习尝试使用base r。谢谢大家。
1 个解决方案
#1
4
You can use make.names(..., unique = TRUE)
to get unique row names
您可以使用make.names(……,unique = TRUE)来获取唯一的行名称
rownames(diasyhoras2) <- make.names(diasyhoras[,1], unique = TRUE)
Here's a quick example of what will happen to the names ...
这里有一个关于名字将会发生什么的简单例子……
rep(month.abb[1:2], 3)
# [1] "Jan" "Feb" "Jan" "Feb" "Jan" "Feb"
make.names(rep(month.abb[1:2], 3), unique = TRUE)
# [1] "Jan" "Feb" "Jan.1" "Feb.1" "Jan.2" "Feb.2"
Unfortunately there is no way around this if you want to use the days as row names of your data frame. In R, as the error states, duplicate row names are not allowed in data frames. They are, however, allowed in matrices so you may want to go that route instead. I am not familiar with the d3heatmap
package so I cannot say whether you would get your desired result if you used a matrix.
不幸的是,如果您想使用天数作为数据帧的行名,则无法解决这个问题。在R中,由于错误状态,在数据帧中不允许重复行名称。然而,它们在矩阵中是允许的所以你可能想要走那条路。我不熟悉d3hotmap软件包,所以我不能说如果你使用一个矩阵你是否会得到你想要的结果。
x <- data.frame(a = rep(month.abb[1:2], 2))
rownames(x) <- x$a
# Error in `row.names<-.data.frame`(`*tmp*`, value = value) :
# duplicate 'row.names' are not allowed
# In addition: Warning message:
# non-unique values when setting 'row.names': ‘Feb’, ‘Jan’
m <- as.matrix(x)
rownames(m) <- x$a
m
# a
# Jan "Jan"
# Feb "Feb"
# Jan "Jan"
# Feb "Feb"
#1
4
You can use make.names(..., unique = TRUE)
to get unique row names
您可以使用make.names(……,unique = TRUE)来获取唯一的行名称
rownames(diasyhoras2) <- make.names(diasyhoras[,1], unique = TRUE)
Here's a quick example of what will happen to the names ...
这里有一个关于名字将会发生什么的简单例子……
rep(month.abb[1:2], 3)
# [1] "Jan" "Feb" "Jan" "Feb" "Jan" "Feb"
make.names(rep(month.abb[1:2], 3), unique = TRUE)
# [1] "Jan" "Feb" "Jan.1" "Feb.1" "Jan.2" "Feb.2"
Unfortunately there is no way around this if you want to use the days as row names of your data frame. In R, as the error states, duplicate row names are not allowed in data frames. They are, however, allowed in matrices so you may want to go that route instead. I am not familiar with the d3heatmap
package so I cannot say whether you would get your desired result if you used a matrix.
不幸的是,如果您想使用天数作为数据帧的行名,则无法解决这个问题。在R中,由于错误状态,在数据帧中不允许重复行名称。然而,它们在矩阵中是允许的所以你可能想要走那条路。我不熟悉d3hotmap软件包,所以我不能说如果你使用一个矩阵你是否会得到你想要的结果。
x <- data.frame(a = rep(month.abb[1:2], 2))
rownames(x) <- x$a
# Error in `row.names<-.data.frame`(`*tmp*`, value = value) :
# duplicate 'row.names' are not allowed
# In addition: Warning message:
# non-unique values when setting 'row.names': ‘Feb’, ‘Jan’
m <- as.matrix(x)
rownames(m) <- x$a
m
# a
# Jan "Jan"
# Feb "Feb"
# Jan "Jan"
# Feb "Feb"