Having trouble changing rownames within a column

This a very small part of the dataset that im working with but,

Under SUBJNO the rowname is CAN1,,,,CAN6. I am trying to change that to say Cancer Patient for all 6 rows. I've tried rownames, rownames.df, but for some reason I keep getting errors. Any help is greatly appreciated. Thanks

SUBJNO X Y
1 CAN1 96.2 2.52
2 CAN2 85.0 4.28
3 CAN3 89.5 3.67
4 CAN4 98.1 3.33
5 CAN5 89.7 3.34
6 CAN6 84.3 3.55

Could you post an example of what you want the final data to look like? I'm not quite understanding but would like to try and help.

It looks like the rownames are 1,2,3,4,5,6. Is this a data.frame?

I want the data under the column SUBNO that currently stores CAN1 through CAN6 to be renamed to the same name "Cancer Patient" for all six rows. Essentially have the name Cancer Patient repeated for six rows. I'm sorry I will update it more when I get home

Are you looking for something like this?


library(tidyverse)

testdat <- tribble(
  ~SUBJNO, ~X, ~Y,
  'CAN1', 96.2, 2.52,
  'CAN2', 85.0, 4.28,
  'CAN3', 89.5, 3.67,
  'CAN4', 98.1, 3.33,
  'CAN5', 89.7, 3.34,
  'CAN6', 84.3, 3.55
)

newdat <- testdat %>%
  mutate(SUBJNO="Cancer Patient")

newdat
#> # A tibble: 6 x 3
#>   SUBJNO             X     Y
#>   <chr>          <dbl> <dbl>
#> 1 Cancer Patient  96.2  2.52
#> 2 Cancer Patient  85    4.28
#> 3 Cancer Patient  89.5  3.67
#> 4 Cancer Patient  98.1  3.33
#> 5 Cancer Patient  89.7  3.34
#> 6 Cancer Patient  84.3  3.55

Created on 2019-12-03 by the reprex package (v0.3.0)

How would I able to rename 500 rows with the same name?

It would be the same method, no matter how large the data. The mutate function updates variables. In this case, it makes SUBJNO="Cancer Patient" for all rows.

Would I have to ~SUBJNO, ~X, ~Y,
'CAN1', 96.2, 2.52,
'CAN2', 85.0, 4.28,
'CAN3', 89.5, 3.67,
'CAN4', 98.1, 3.33,
'CAN5', 89.7, 3.34,
'CAN6', 84.3, 3.55
)
Would I need to insert [1:500] within the mutate

That part of the code is just sample data for reproducibility purposes, you don't need to type the values, you just have to replace testdat for the name of your actual data set.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.