How do I name the row names column in r
It sounds like you want to convert the rownames to a proper column of the data.frame. eg:
# add the rownames as a proper column
myDF <- cbind(Row.Names = rownames(myDF), myDF)
myDF
# Row.Names id val vr2
# row_one row_one A 1 23
# row_two row_two A 2 24
# row_three row_three B 3 25
# row_four row_four C 4 26
If you want to then remove the original rownames:
rownames(myDF) <- NULL
myDF
# Row.Names id val vr2
# 1 row_one A 1 23
# 2 row_two A 2 24
# 3 row_three B 3 25
# 4 row_four C 4 26
Alternatively, if all of your data is of the same class (ie, all numeric, or all string), you can convert to Matrix and name the dimnames
myMat <- as.matrix(myDF)
names(dimnames(myMat)) <- c("Names.of.Rows", "")
myMat
# Names.of.Rows id val vr2
# row_one "A" "1" "23"
# row_two "A" "2" "24"
# row_three "B" "3" "25"
# row_four "C" "4" "26"
Putting row names and column names when converting from list to data frame
In LWfunction
return a 1-row dataframe with all the required values in it.
library(FSA)
library(FSAdata)
library(dplyr)
library(tidyr)
LWfunction <- function(x) {
fits <- lm(log(weight) ~ log(length), data = x)
a <- hoCoef(fits, 2,3)
b <- confint(fits)
output <- cbind(a, data.frame(intercept_2.5 = b[1, 1],
intercept_97.5 = b[1, 2],
log_length_2.5 = b[2, 1],
log_length_97.5 = b[2, 2]))
return(output)
}
apply it for each year
and month
:
result <- data %>%
group_by(month, year) %>%
summarise(output = list(LWfunction(cur_data()))) %>%
ungroup %>%
unnest(output)
result
# A tibble: 7 x 13
# month year term `Ho Value` Estimate `Std. Error` T df
# <int> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 4 1992 2 3 3.00 0.0396 0.0216 58
#2 5 1992 2 3 2.88 0.0315 -3.78 64
#3 6 1992 2 3 2.86 0.0317 -4.43 152
#4 7 1992 2 3 2.87 0.0189 -7.11 147
#5 8 1992 2 3 2.89 0.0312 -3.40 67
#6 9 1992 2 3 3.02 0.0326 0.680 114
#7 10 1992 2 3 3.00 0.0314 -0.113 64
# … with 5 more variables: `p value` <dbl>, intercept_2.5 <dbl>,
# intercept_97.5 <dbl>, log_length_2.5 <dbl>,
# log_length_97.5 <dbl>
Is there a way to name column names or rownames?
To construct an array with named dimnames
, pass a named list as the dimnames
argument of matrix
or array
:
x <- array(seq_len(18L), dim = c(3L, 3L, 2L),
dimnames = list(D1 = letters[1:3], D2 = letters[4:6], D3 = letters[7:8]))
x
## , , D3 = g
##
## D2
## D1 d e f
## a 1 4 7
## b 2 5 8
## c 3 6 9
##
## , , D3 = h
##
## D2
## D1 d e f
## a 10 13 16
## b 11 14 17
## c 12 15 18
To modify an existing array so that it has named dimnames
, set the names
attribute of the dimnames
attribute:
y <- array(seq_len(18L), dim = c(3L, 3L, 2L),
dimnames = list(letters[1:3], letters[4:6], letters[7:8]))
names(dimnames(y)) <- c("D1", "D2", "D3")
identical(x, y)
## [1] TRUE
However, note that names(dimnames(y)) <- value
will not work if dimnames(y)
is NULL
:
z <- array(seq_len(18L), dim = c(3L, 3L, 2L))
names(dimnames(z)) <- c("D1", "D2", "D3")
## Error in names(dimnames(z)) <- c("D1", "D2", "D3") :
## attempt to set an attribute on NULL
To get named but "empty" dimnames
in the above case, you would have to do something like
dimnames(z) <- list(D1 = NULL, D2 = NULL, D3 = NULL)
or equivalently
dimnames(z) <- setNames(vector("list", 3L), c("D1", "D2", "D3"))
Now:
z
## , , 1
##
## D2
## D1 [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
##
## , , 2
##
## D2
## D1 [,1] [,2] [,3]
## [1,] 10 13 16
## [2,] 11 14 17
## [3,] 12 15 18
It is interesting that we don't see D3 = 1
and D3 = 2
in the print
output. That might be a bug - I'd have to ask the people upstairs.
Convert row names into first column
Or you can use tibble
's rownames_to_column
which does the same thing as David's answer:
library(tibble)
df <- tibble::rownames_to_column(df, "VALUE")
Note: The earlier function called add_rownames()
has been deprecated and is being replaced by tibble::rownames_to_column()
Get the column name and row name of a specific value in a dataframe
You may use which
with arr.ind = TRUE
to get row and column number where 5 is present. This can be changed to row and column names.
mat <- which(df == 5, arr.ind = TRUE)
paste('row : ', rownames(df)[mat[, 1]], 'column :', colnames(df)[mat[, 2]])
#[1] "row : 200 column : b"
get value using rowname and column name in R?
It is not a row name attribute, but a column 'A' So, create a logical index with the 'A' column, use that as row index, while specifying the column as the 'columnName'
df[df$A == 'row Name 1', 'columnName']
R: count times per column a condition is met and row names appear in a list
We may do this with rowwise
library(dplyr)
df2 %>%
rowwise %>%
mutate(x = +(sum(df1[[rownames]][df1$rownames %in% x]) >= 5),
y = +(sum(df1[[rownames]][df1$rownames %in% y]) >= 5)) %>%
ungroup
-output
# A tibble: 3 × 5
rownames batch totalcount x y
<chr> <chr> <int> <int> <int>
1 sample1 a 10 1 0
2 sample2 b 15 1 1
3 sample3 a 6 0 1
Or based on the data, a base R
option would be
out <- aggregate(. ~ grp, FUN = sum,
transform(df1, grp = c('x', 'y')[1 + (rownames %in% y)] )[-1])
df2[out$grp] <- +(t(out[-1]) >= 5)
-output
> df2
rownames batch totalcount x y
1 sample1 a 10 1 0
2 sample2 b 15 1 1
3 sample3 a 6 0 1
data
df1 <- structure(list(rownames = c("m1", "m2", "m3", "m4"), sample1 = c(0L,
1L, 6L, 3L), sample2 = c(5L, 7L, 2L, 1L), sample3 = c(1L, 5L,
0L, 0L)), class = "data.frame", row.names = c(NA, -4L))
df2 <- structure(list(rownames = c("sample1", "sample2", "sample3"),
batch = c("a", "b", "a"), totalcount = c(10L, 15L, 6L)),
class = "data.frame", row.names = c(NA,
-3L))
Related Topics
How to Deal with "'Somefunction' Is Not an Exported Object from 'Namespace:Somepackage'" Error
R Compare Multiple Values with Vector and Return Vector
How to Work with Large Numbers in R
Replacing Numbers Within a Range with a Factor
Unicode Characters in Ggplot2 PDF Output
Rolling Join on Data.Table with Duplicate Keys
Using Cut and Quartile to Generate Breaks in R Function
Getting Strings Recognized as Variable Names in R
What's the Best Way to Use R Scripts on the Command Line (Terminal)
File Path Issues in R Using Windows ("Hex Digits in Character String" Error)
Add Error Bars to Show Standard Deviation on a Plot in R
How to Place Grobs with Annotation_Custom() at Precise Areas of the Plot Region
How to Plot with a Png as Background
How to Add Table of Contents in Rmarkdown
Why Is 'Vapply' Safer Than 'Sapply'