Convert Row Names into First Column

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()

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 row names into a column in Pandas

df.index.name = 'newhead'
df.reset_index(inplace=True)

yields

  newhead  head1  head2  head3
0 bar 32 3 100
1 bix 22 NaN NaN
2 foo 11 1 NaN
3 qux NaN 10 NaN
4 xoo NaN 2 20

How to convert row names to column names and bind it by order in r

Assuming your long-form data starts with the rows in the order you want, try this:

month_order = unique(returns)
returns.df <- returns %>%
spread(key = DATE, value = RETURN) %>%
select(c("TICKER", month_order))

If your data doesn't begin in the right order, append a year and convert it to a Date class object. Then you can sort it to the right order and use the method above.

How to convert rownames to columns in a list of elements

Create a dataframe in each list and remove the rownames -

lapply(results1, function(x) {
tmp <- data.frame(names=names(x), x)
rownames(tmp) <- NULL
tmp
})

#[[1]]
# names x
#1 ..a15.pdf 1.27679608
#2 ..a17.pdf 1.05090176
#3 ..a18.pdf 1.51820192
#4 ..a21.pdf 2.30296037
#5 ..a2TTT.pdf 1.48568732
#6 ..a5.pdf 0.49371310
#7 ..B11.pdf 1.02705905
#8 ..B12.pdf 0.99974736
#...
#...

#[[2]]
# names x
#1 ..a15.pdf 2.0966876
#2 ..a17.pdf 2.1982604
#3 ..a18.pdf 1.9181420
#4 ..a21.pdf 0.8014485
#5 ..a2TTT.pdf 2.1616956
#...
#...

A shorter alternative would be -

lapply(results1, stack)

Convert row names in multiple data frames to column in data frame

Youve got a dataframe with columns named "w,x,y,z" . Just do

data$names <- rownames(data) 

to add a new column.

edit

In response to Boogie's query, here's lapply with an anonymous function to do the loop.

   foo = as.data.frame(matrix(1:15,3,5))
rownames(foo) <-c('frist','prime','lastly')
foo
bar = list(foo,t(foo), rbind(foo,foo))
bar[[1]] = as.data.frame( foo)
bar[[2]] =data.frame( t(foo))
bar[[3]] = data.frame(rbind(foo,foo))
bar
bar = lapply(bar,FUN= function(x) { x$date <-rownames(x);return(x)})
bar

Convert row names into new columns in a data frame

To separate that with the first "dot" you can use:

Mesure %>%
separate(Row.names, sep = "\\.", into = c("Sample_type", "Locality"), extra = "merge")

Explanation:

  • You don't need to convert rownames_to_column(), because "Row.names" is already a column.
  • sep = "." is not enough as the . is taken as a regular expression.
  • There are many . in the column, so you need to specify extra = "merge" to separate only at first appearance. If you would like to keep only "Paris" without AG-110m etc, you specify extra = "drop" there.

Result with extra = "merge":

   Sample_type        Locality mean_Mesure max_Mesure min_Mesure
1 Aquatic_moss Paris.AG-110m.< 100 110 90
2 Aquatic_moss Paris.BE-7. 123 177 53
3 Aquatic_moss Paris.CO-57.< 40 60 20
4 Aquatic_moss Paris.CO-58.< 40 50 30
5 Aquatic_moss Paris.CO-60.< 50 70 30
6 Aquatic_moss Paris.CS-134.< 200 300 100

Result with extra = "drop":

   Sample_type Locality mean_Mesure max_Mesure min_Mesure
1 Aquatic_moss Paris 100 110 90
2 Aquatic_moss Paris 123 177 53
3 Aquatic_moss Paris 40 60 20
4 Aquatic_moss Paris 40 50 30
5 Aquatic_moss Paris 50 70 30
6 Aquatic_moss Paris 200 300 100

If you need to drop "<" at the end of Locality column, run something like:

Mesure$Locality <- gsub("<$", "", Mesure$Locality)

where "<$" means "< at the end of the string".

How to convert a column to row.names from a tibble

First, you can see the difference

> str(entrepreneur[, 1])
tibble [5 x 1] (S3: tbl_df/tbl/data.frame)
$ Factors: chr [1:5] "Competition" "Cultural Support" "Financing" "High Growth" ...

> str(entrepreneur[[1]])
chr [1:5] "Competition" "Cultural Support" "Financing" "High Growth" ...

Try the code below (using entrepreneur[[1]] rather than entrepreneur[,1])

> `rownames<-`(as.data.frame(entrepreneur[-1]), entrepreneur[[1]])
Baden-Wⁿrttemberg Bayern Berlin Brandenburg
Competition 0.71 0.67 1.00 1.00
Cultural Support 0.66 0.66 0.56 0.55
Financing 0.81 0.83 0.90 0.64
High Growth 0.62 0.77 0.82 1.00
Human Capital 0.46 0.49 0.79 0.77


Related Topics



Leave a reply



Submit