2
$\begingroup$

I'm trying to create a new data frame that contains the row names and the first column of an existing data : i tried this

#To take the rownames of my old data
New_data <- as.data.frame(row.names(old_data))
#To add the first column of my old data to a new data
New_data <- cbind(old_data[,1])

When i visualize my new data using View(New_Data) i don't see the name of my the first column just V1

example

> old_data
   NAME  AGE  SEXE  
A   AQ   22    M
B   RT   14    M
C   DS   26    F
D   YY   19    M
E   IO   32    F
F   PP   20    F

New_data <- as.data.frame(row.names(old_data))
New_data 
A
B
C
D
E
F

#add the first column of my old data to a new data

New_data <- cbind(old_data[,1])
>New_data
       V1
A      22
B      14
C      26
D      19
E      32
F      20

As you can see the name of the column in my new data is V1 , i want to bind the first column of my old data with the name of this column like this

>New_data
           Name
    A      22
    B      14
    C      26
    D      19
    E      32
    F      20
$\endgroup$

1 Answer 1

2
$\begingroup$

The issue is not with cbind but rather with old_data[,1]. Since you are selecting only 1 column R will convert it into a vector, and a vector has no column name. Try cbind(old_data[,1,drop=F]).

$\endgroup$
1
  • $\begingroup$ It works, thank you very much $\endgroup$
    – Anouar
    Oct 22, 2021 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.