create virtual patient data set [duplicate] - r

This question already has answers here:
Unique combination of all elements from two (or more) vectors
(6 answers)
Closed 3 years ago.
I would like to create a virtual patient data set in R , where in I would like to have the following time points TIME = seq(1, 10, 1), be repeated for 3 unique patient IDs

Thanks to the comment from #markus, you can use expand.grid:
df <- expand.grid(TIME = 1:10, ID = 1:3)

Related

consolidate rows with same value using R [duplicate]

This question already has answers here:
How to sum a variable by group
(18 answers)
Closed 4 years ago.
I have a CSV file where there are 4 columns. I would like to get answer by adding the 4th column values where the 3rd column values are same.
The data that i have looks like this:
Now i want to aggregate the above data like this:
Anyone can help me with your ideas!
Using aggregate would do the trick here. Below I'm summing the value variable using id as the group (notice ids 6 and 10 are repeating).
df <- data.frame(id = c(1,2,3,4,5,6,6,7,8,9,10,10),
value = c(9,5,6,8,4,3,2,5,3,5,1,2))
df_sum <- aggregate(value ~ id, data=df, FUN=sum)

How to count the number of occurence of First Charcter of each string of a column in R [duplicate]

This question already has answers here:
Counting unique / distinct values by group in a data frame
(12 answers)
Closed 4 years ago.
I have a data set which has a single column containing multiple names.
For eg
Alex
Brad
Chrisitne
Alexa
Brandone
And almost 100 records like this. I want to display record as
A 2
B 2
C 1
Which means i need to show this frequency from higher to lower and if there is a tie breaker , the the values should be shown in Alphabetical Order .
I have been trying to solve this but i am not able to.
Any pointer on these ?
df <- data.frame(name = c("Alex", "Brad", "Brad"))
first_characters <- substr(df$name, 1, 1)
result <- sort(table(first_characters), decreasing = TRUE)
# from wide to long
data.frame(result)

Find total frequency of number In Column [duplicate]

This question already has answers here:
Counting the number of elements with the values of x in a vector
(20 answers)
Closed 5 years ago.
I am new on R. I want to ask, How to find frequency of each Number in Column, there are multiple numbers in column. i want to frequency of each number. I want just simple code. You can imagine that data set name is Oct-TT. Thanks
Here is the answer:
df <- as.data.frame(sample(10:20, 20,replace=T))
colnames(df) <- "Numbers"
View(df)
as.data.frame(table(df$Numbers))

R sum on rows based on day [duplicate]

This question already has answers here:
How to sum a variable by group
(18 answers)
Closed 5 years ago.
I have a dataframe containing sells by day, but as there are many values for one day, so i'd like to rebuilt my dataframe to have just one value per day which is the sum of all the sells of this day.
what i have:
what i'd like to have:
(the values are not well calculated but it's for the example)
I tried agregate and functions like this but it dosn't work and I dont know how to do this...
Thanks for help
Aggregate should work
aggregate(TOT_OP_MAIN_PAID_MNT,by=list(DATE_ID),FUN=sum)
This should work
df <- data.frame(DATE_ID = 1, TOT_OP_MAIN_PAID_MNT = 1)
aggregate(TOT_OP_MAIN_PAID_MNT ~ DATE_ID, df, sum)

Removing rows by reference using data.table? [duplicate]

This question already has answers here:
How to delete a row by reference in data.table?
(7 answers)
Closed 8 years ago.
I am trying to figure out how to remove a group of rows from a dataset by reference. For example, with this data set:
testset <- data.table(date=as.Date(c("2013-07-02","2013-08-03","2013-09-04","2013-10-05","2013-11-06")),
yr = c(2013,2013,2013,2013,2013),
mo = c(07,08,09,10,11),
da = c(02,03,04,05,06),
plant = LETTERS[1:5],
product = as.factor(letters[26:22]),
rating = runif(25))
I want to remove all rows where the product is "y". I have no idea how to go about this.
you can use either of the following commands -
testset_new <- subset(testset,product!="y")
or
testset_new <- testset[testset$product!="y",]

Resources