简体   繁体   中英

R , Replicating the rownames in data.frame

I have a data.frame with dimension [6587 37] and the rownames must repeat after every 18 rows. How i can do this in Rstudio.

Row names in data.frames have to be unique.

> df <- data.frame(x = 1:2)
> rownames(df) <- c("a", "a")
Error in `row.names<-.data.frame`(`*tmp*`, value = value) : 
  duplicate 'row.names' are not allowed
In addition: Warning message:
non-unique value when setting 'row.names': ‘a’

You could use make.names to make the names unique, but still carry some repeating information.

> make.names(c("a","a"), unique = TRUE)
[1] "a"   "a.1"

These could be identified with help from grep Or you could make a column in df or a second data.frame that holds the information

If your 18 column names are:

mynames <- c("a", "b", "c", "d", "e", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s")

You can get what you want with:

paste0(rep(mynames,length.out=6587),rep(1:366,each=18,length.out=6587))

Or you can modify the names pasting different things.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM