rep

Add rep vector to dataframe with uneven total rows

空扰寡人 提交于 2021-02-17 06:05:24
问题 I'm trying to find a way of automating a large dataset to add two factors but the data may contain uneven rows. I've tried to do this with the 'rep' function but this will only work if the data frame has even numbers. x<-c(1,3,5,7,9) y<-c(2,4,6,8,10) df<-data.frame(x,y) df$state<-factor(rep(1:2)) Error in `$<-.data.frame`(`*tmp*`, state, value = 1:2) : replacement has 2 rows, data has 5 How do I get the data.frame to recycle 1 into row 5 instead of an error? 回答1: rep() 's length.out argument

Replicate each element in a vector different times in R [duplicate]

ε祈祈猫儿з 提交于 2020-07-30 09:24:53
问题 This question already has an answer here : More than one value for “each” argument in “rep” function (R) ? (1 answer) Closed 2 months ago . Suppose I have a numeric vector v v <- 1:5 I want to rep v[1] by v[1] times. v[2] by v[2] times... and so on.... The desired output would be: 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 The following does not work. Got any ideas? rep(v, each = function(x) v[x]) Many thanks in advance. 回答1: We can use rep on itself rep(v, v) If we want to specify the argument, use times

R : Assigning students to equal groups with random sampling. Understanding rep() argument length.out to sample()

我的未来我决定 提交于 2020-01-16 09:01:09
问题 I have 33 students I want to sort into groups of 6 (or as close as possible) on 5 different occasions. So I assign a number between 1 and 6 to the students on different occassions. I've managed the following: studentlist <- data.frame(seq(1:33)) studentlist$Occassion1 <- sample(factor(rep(1:6, length.out=nrow(studentlist)), labels=paste0(1:6))) studentlist$Occassion2 <- sample(factor(rep(1:6, length.out=nrow(studentlist)), labels=paste0(1:6))) studentlist$Occassion3 <- sample(factor(rep(1:6,

Column of sequence with repeated elements based on column with dates [duplicate]

偶尔善良 提交于 2019-12-25 12:48:12
问题 This question already has answers here : Increment by 1 for every change in column (4 answers) Closed 2 years ago . Consider a data frame ordered by a column with dates: df=data.frame(event=1:12, subject=rep("M325",12), date=c(rep("2017-11-01",4),rep("2017-11-14",8))) What I want is to create a fourth column with a sequence from 1 to the next unique date, with every element in the sequence repeated every i- th date. For example: event subject date num 1 1 M325 2017-11-01 1 2 2 M325 2017-11-01

setCellStyle - apply cell style (percent) to matrix using XLConnect

拥有回忆 提交于 2019-12-12 05:17:51
问题 My question is based on an issue mentioned in a previous question Formatting of numbers using Mirai's XLConnect. I have trouble implementing this solution from @joran and I think I might not be the only person with this problem. I want to export a correlation matrix(10x10) to excel. It is saved as a matrix called export. library(XLConnect) wb <- loadWorkbook(paste0("corr_test.xlsx"), create = TRUE) prcntg <- createCellStyle(wb) setDataFormat(prcntg, format = "0.00%") createSheet(wb, name=

Sort R rows based on the number of repetition

筅森魡賤 提交于 2019-12-11 14:19:21
问题 I have a table like: me mine 1 z ghm 2 d gwm 3 d gom 4 d gum 5 f gom 6 g gum 7 h gom 8 t ghm 9 y gom 10 u gom how can I sort these data based on the repetition in mine clumn, note: all start with "g" and end with "m". the result im looking for is like" me mine 1 d gom 2 f gom 3 h gom 4 y gom 5 u gom 6 d gum 7 g gum 8 t ghm 9 z ghm 10 d gwm or somthing like this: gom d,f,h,y,u gum d,g, ghm t,z gwm z,d 回答1: Try this ( df is your data.frame): indices<-match(df$mine,names(sort(table(df$mine)

repeat rows in a dataset based on a column, but increment the rows [duplicate]

我的未来我决定 提交于 2019-12-11 03:44:43
问题 This question already has answers here : Repeat each row of data.frame the number of times specified in a column (7 answers) Closed 2 years ago . I have a dataset which has project name, start year and contract term. I need to develop this dataset into time series. For example, one row in my dataset is: Project A, start year 2003 and contract term 5. I would like to repeat each row based on contract term. My dataset looks like this: Project Name Start Year Contract Term A 2003 5 B 2013 3 C

R - Make a repetitive sequence with 'rep'

五迷三道 提交于 2019-12-10 19:52:23
问题 I'm wondering if there is a simpler way to make a list with, for example 10 '4', 20 '6' and 30 '3' then writing it by hand ( example <- c(4,4,4,4,...) ) with the function 'rep'. I know i can repeat a certain sequence n times and each by n times too, but i don't know how can i make one with different amounts of each number. 回答1: Just use rep with both arguments being the desired vectors: x <- rep(c(4, 6, 3), c(10, 20, 30)) table(x) 3 4 6 30 10 20 来源: https://stackoverflow.com/questions

R rep seq where number of rows is not multiple of seq length

余生颓废 提交于 2019-12-09 01:42:53
问题 I have a data frame with 1666 rows. I would like to add a column with a repeating sequence of 1:5 to use with cut() to do cross validation. It would look like this: Y x1 x2 Id1 1 .15 3.6 1 0 1.1 2.2 2 0 .05 3.3 3 0 .45 2.8 4 1 .85 3.1 5 1 1.01 2.9 1 ... ... ... ... I've tried the following 2 ways but get an error message as it seems to only add numbers in increments of the full seq() argument: > tr2$Id1 <- rep(seq(1,5,1), (nrow(tr2)/5)) Error in `$<-.data.frame`(`*tmp*`, "Id", value = c(1, 2,

Creating a repeating vector sequence in R [duplicate]

99封情书 提交于 2019-12-01 11:56:22
This question already has an answer here: R: generate a repeating sequence based on vector 1 answer I need some help. How to create the following vector sequence: 1 1 1 1 2 2 2 3 3 4 I tried to use (rep) and (seq) but still unsucessfull. Try this: rep(1:4,4:1) Output: [1] 1 1 1 1 2 2 2 3 3 4 or less concisely: c(rep(1, 4), rep(2,3), rep(3, 2), 4) output: [1] 1 1 1 1 2 2 2 3 3 4 来源: https://stackoverflow.com/questions/46167499/creating-a-repeating-vector-sequence-in-r