R move index column to first column [duplicate] - r

This question already has answers here:
Convert row names into first column
(9 answers)
Closed 6 years ago.
I have following data frame:
RMSE
A 0.03655830
B 0.24513014
C 0.02009853
D 0.02223135
I want to move column that has A,B,C,D to be the first column and add an index to the data.frame.

try this:
df <- cbind(newColName = rownames(df), df)
rownames(df) <- 1:nrow(df)
hope this is what you meant, the result will be:
newColName RMSE
1 A 0.03655830
2 B 0.24513014
3 C 0.02009853
4 D 0.02223135

Related

split the lines of a data frame into a variable number of lines based on a character in R [duplicate]

This question already has answers here:
Split delimited strings in a column and insert as new rows [duplicate]
(6 answers)
Split comma-separated strings in a column into separate rows
(6 answers)
Closed 10 months ago.
I have this df:
df = data.frame(ID = c(1,2,3),
A = c("h;d;c", "j;k", "k"))
And i want to retrieve a new df with splited rows based on ";" character, just like this:
ID A
1 1 h
2 1 d
3 1 c
4 2 j
5 2 k
6 3 k
I searched for other questions, but they need an exact amount of expected characters. (Split data frame string column into multiple columns)
Thanks for the help!

Finding the maximum value for each row and extract column names [duplicate]

This question already has answers here:
R Create column which holds column name of maximum value for each row
(4 answers)
Closed 1 year ago.
Say we have the following matrix,
x <- matrix(1:9, nrow = 3, dimnames = list(c("X","Y","Z"), c("A","B","C")))
What I'm trying to do is:
1- Find the maximum value of each row. For this part, I'm doing the following,
df <- apply(X=x, MARGIN=1, FUN=max)
2- Then, I want to extract the column names of the maximum values and put them next to the values. Following the reproducible example, it would be "C" for the three rows.
Any assistance would be wonderful.
You can use apply like
maxColumnNames <- apply(x,1,function(row) colnames(x)[which.max(row)])
Since you have a numeric matrix, you can't add the names as an extra column (it would become converted to a character-matrix).
You can choose a data.frame and do
resDf <- cbind(data.frame(x),data.frame(maxColumnNames = maxColumnNames))
resulting in
resDf
A B C maxColumnNames
X 1 4 7 C
Y 2 5 8 C
Z 3 6 9 C

Remove duplicated rows in dataframe in R [duplicate]

This question already has answers here:
Delete duplicate rows in two columns simultaneously [duplicate]
(2 answers)
Closed 3 years ago.
I have a following problem:
My dataframe has a lot of columns. I would like to remove rows that have same values in column X, Y and Z.
See my dataframe:
A B C X Y Z
1 2 3 4 5 6
2 5 4 4 5 6
In the dataframe above I would like to delete the first row, because X, Y and Z are the same in both rows.
I tried this, but it returned me something different:
newtable <- df[!duplicated(df$X, df$Z, df$Z), ]
Thanks a lot!
According to ?duplicated, the usage is
duplicated(x, incomparables = FALSE, ...)
where
x- a vector or a data frame or an array or NULL.
i.e. it cannot take more than one argument for 'x'. An option is to subset the dataset columns and apply as x
df[!duplicated(df[c("X", "Y", "Z")]), ]

How to create a variable using another variable as an Index? [duplicate]

This question already has answers here:
Using row-wise column indices in a vector to extract values from data frame [duplicate]
(2 answers)
Closed 3 years ago.
I'm looking to create a new variable, d, which grabs the value from either an or b based off of the variable C.
dat = data.frame(a=1:10,b=11:20,c=rep(1:2,5))
The result would be:
d = c(1,12,3,14,... etc)
We can use a row/column indexing where the row index is the sequence of rows and column index the 'c' column, cbind them and extract the elements from the dataset based on this
dat$d <- dat[1:2][cbind(seq_len(nrow(dat)), dat$c)]
dat$d
#[1] 1 12 3 14 5 16 7 18 9 20
NOTE: This should also work when there are multiple column values to extract.
You can do
dat$d <- ifelse(dat$c==1,dat$a,dat$b)
A dplyr variant
dat %>%
mutate(d = case_when(c==1 ~ a,
TRUE ~ b))

R, accessing a column vector of a matrix by name [duplicate]

This question already has answers here:
Extract matrix column values by matrix column name
(2 answers)
Closed 7 years ago.
In R I can access the data in a column vector of a column matrix by the following:
mat2[,1]
Each column of mat2 has a name. How can I retrieve the data from the first column by using the name attribute instead of [,1]?
For example suppose my first column had the name "saturn". I want something like
mat2[,1] == mat2[saturn]
The following should do it:
mat2[,'saturn']
For example:
> x <- matrix(1:21, nrow=7, ncol=3)
> colnames(x) <- paste('name', 1:3)
> x[,'name 1']
[1] 1 2 3 4 5 6 7
Bonus information (adding to the first answer)
x[,c('name 1','name 2')]
would return two columns just as if you had done
x[,1:2]
And finally, the same operations can be used to subset rows
x[1:2,]
And if rows were named...
x[c('row 1','row 2'),]
Note the position of the comma within the brackets and with respect to the indices.

Resources