Convert the values in a column into row names in an existing data frame
This should do:
samp2 <- samp[,-1]
rownames(samp2) <- samp[,1]
So in short, no there is no alternative to reassigning.
Edit: Correcting myself, one can also do it in place: assign rowname attributes, then remove column:
R> df<-data.frame(a=letters[1:10], b=1:10, c=LETTERS[1:10])
R> rownames(df) <- df[,1]
R> df[,1] <- NULL
R> df
b c
a 1 A
b 2 B
c 3 C
d 4 D
e 5 E
f 6 F
g 7 G
h 8 H
i 9 I
j 10 J
R>
Convert first column in data.frame to row index
First your data example.
mydf <-
structure(list(target_id = c("ENST00000000233", "ENST00000000412",
"ENST00000000442", "ENST00000001008", "ENST00000001146", "ENST00000002125"
), sample1 = c(9L, 0L, 0L, 0L, 0L, 0L), sample10 = c(0L, 0L,
0L, 0L, 0L, 0L), sample100 = c(3499.51, 0, 0, 0, 0, 0), sample101 = c(0L,
0L, 0L, 0L, 0L, 0L), sample102 = c(0L, 0L, 0L, 0L, 0L, 0L), sample103 = c(0L,
0L, 0L, 0L, 0L, 0L)), .Names = c("target_id", "sample1", "sample10",
"sample100", "sample101", "sample102", "sample103"), class = "data.frame", row.names = c("1:",
"2:", "3:", "4:", "5:", "6:"))
Now the code.
result <- mydf[-1]
row.names(result) <- mydf$target_id
result
sample1 sample10 sample100 sample101 sample102 sample103
ENST00000000233 9 0 3499.51 0 0 0
ENST00000000412 0 0 0.00 0 0 0
ENST00000000442 0 0 0.00 0 0 0
ENST00000001008 0 0 0.00 0 0 0
ENST00000001146 0 0 0.00 0 0 0
ENST00000002125 0 0 0.00 0 0 0
Simple, no?
Convert column names to row values of a new column and fill their values to another new column
We just need to pivot to long
library(tidyr)
pivot_longer(Unp, cols = -Week, names_to = "Type", values_to = "Count")
R data frame convert column names to rows based on dates, the column names which has a common string and there non 0 values to 1 row item
We can use pivot_longer
to reshape the data into 'long' format and then with filter
remove any rows having both x and y values as 0
library(dplyr)
library(dplyr)
df1 %>%
pivot_longer(cols = -date, names_to = c(".value", "colname"),
names_sep = "_", values_drop_na = TRUE)%>%
filter(if_any(c(x, y), ~ . > 0))
-output
# A tibble: 5 x 4
# date colname x y
# <chr> <chr> <dbl> <dbl>
#1 01-01-2021 a01 1 2
#2 01-01-2021 b01 0 4
#3 01-01-2021 d01 3 4
#4 02-01-2021 b01 3.1 1.1
#5 02-01-2021 c01 4.5 6.2
data
df1 <- structure(list(date = c("01-01-2021", "02-01-2021"), x_a01 = c(1,
0), y_a01 = c(2, 0), x_b01 = c(0, 3.1), y_b01 = c(4, 1.1), x_c01 = c(0,
4.5), y_c01 = c(0, 6.2), x_d01 = c(3, 0), y_d01 = c(4, 0)),
class = "data.frame", row.names = c(NA,
-2L))
How do I convert a column in my data into the rows in R
You can use tibble::column_to_rownames
.
library(tibble)
tibble::column_to_rownames(df, "country")
Output
score code prev.score
Brazil 1 BRA 5
Singapore 2 SIN 3
France 3 FRA 4
Data
df <-
structure(
list(
score = c(1, 2, 3),
country = c("Brazil", "Singapore",
"France"),
code = c("BRA", "SIN", "FRA"),
prev.score = c(5, 3,
4)
),
class = "data.frame",
row.names = c(NA,-3L)
)
Create new data frame with column names as row names, and values from one column as new column names
Example using base t() and tidyverse + magrittr
k <- tibble(Station = 1:4,
year = c(2016,2016,2016,2017),
month = c(1,3,7,2),
rise = c(334,348,242,445),
set = c(1042,1053,1153,962),
hrday = c("X1.134","X14.22", "X23.233","X10.753"))
new_df <- k %>% select(-Station,-hrday) %>% t() %>% as_tibble() %>%
magrittr::set_colnames(c(k$hrday)) %>%
mutate(var =subset(colnames(k),!colnames(k) %in% c("hrday","Station"))) %>%
dplyr::select(var, X1.134:X10.753)
new_df
convert columns to row and rows to columns in a data frame
Reshape the data twice :
library(tidyr)
dataInit %>%
pivot_longer(cols = -date) %>%
pivot_wider(names_from = date, values_from = value)
# name `2021-03-14` `2021-03-15`
# <chr> <dbl> <dbl>
#1 CONG_TCH 5.7 3.5
#2 CONG_SDCCH 2.3 4.6
#3 ACC 4.5 10.2
#4 RET 3.5 2.6
#5 SER 19.5 56.2
Or with data.table
:
library(data.table)
dcast(melt(setDT(dataInit),id.vars = 'date'), variable~date, value.var = 'value')
Related Topics
R on Macos Error: Vector Memory Exhausted (Limit Reached)
Dplyr Mutate Rowsums Calculations or Custom Functions
Use of Ggplot() Within Another Function in R
Read All Files in Directory and Apply Multiple Functions to Each Data Frame
Create a Data.Frame Where a Column Is a List
Using Functions of Multiple Columns in a Dplyr Mutate_At Call
Offline Install of R Package and Dependencies
Number of Months Between Two Dates
How Does Predict.Lm() Compute Confidence Interval and Prediction Interval
Create Sequence of Repeated Values, in Sequence
Reshape Multiple Values At Once
How to Install a Package That Has Been Archived from Cran
Conditional Merge/Replacement in R
Idiomatic R Code For Partitioning a Vector by an Index and Performing an Operation on That Partition
Invalid Multibyte String in Read.Csv
Error in ≪My Code≫: Target of Assignment Expands to Non-Language Object