Use a 'for' loop for the 'aggregate' command? [duplicate] - r

This question already has answers here:
Mean per group in a data.frame [duplicate]
(8 answers)
Closed 7 years ago.
I've got a data frame as so,
Treatment Type Numerical Value
1 A 3
1 B 2
1 A 8
1 B 7
2 B 4
2 B 1
2 A 2
2 A 2
I want to make a table of means for each type and treatments.
Using aggregate, I have: aggregate(df[,3], list(Treatment) ,mean) which gives me the means for each treatment but not separated by type too. I was thinking this could be rectified by a for-loop.
Note: This is just a subset of the data, and the list of numerical values is hundreds for each type and treatment.

Since I don't have repu to comment:
aggregate(df, list(Treatment,Type), mean)

Related

Is there a way in R to make all possible combinations between rows of different columns? [duplicate]

This question already has answers here:
Unique combination of all elements from two (or more) vectors
(6 answers)
Generate list of all possible combinations of elements of vector
(10 answers)
Closed 2 years ago.
I have a df with one column and I would like to make combinations with the values of this column in order to have a new df with two columns, like he simple example below: (Obs: my df has ~5000 rows)
df
CG
1
2
3
##I would like a result similar to this:
> head(df1)
C1 C2
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
Does someone could help me?
Thank you in advance

Making 4 columns into 1 column in R [duplicate]

This question already has answers here:
Reshaping multiple sets of measurement columns (wide format) into single columns (long format)
(8 answers)
Reshape a dataframe to long format with multiple sets of measure columns [duplicate]
(3 answers)
Closed 4 years ago.
I have 3 tables that looks something like this (but with 40,000+ observations and 40 variables)
in1 out1 in2 out2 in3 out3
1 2 2 4 3 5
1 3 2 5 3 6
1 3 2 6 3 7
I want to take columns out1, out2, and out3 and make them one column, then create a new table that looks like this:
in out
1 2
1 3
1 3
2 4
2 5
2 6
3 5
3 6
3 7
So basically I want to take 3 huge tables I have, combine them into 1 table and then merge (stack? I don't know the correct wording) 3 specific columns together into 1 column with a new name.
I've tried a few methods such as:
table$out <- cbind(table1$out1, table2$out2, table3$out3)
but I get errors like this:
Error in `$<-.data.frame`(`tmp`, out, value = c(0.98, 0.59, 0.69, :
replacement has 31467 rows, data has 42141
number of rows of result is not a multiple of vector length (arg 1)
I'm sorry if this is a very simple question.. I might just be overthinking it

Missing values in sequence into actual sequence in R? [duplicate]

This question already has answers here:
How to create a consecutive group number
(13 answers)
Closed 5 years ago.
I have a vector of integers, for example, v <- c(1,5,1,2,2,4,7,5,7). If I sort(unique(v)), the values 3 and 6 would be missing in the sequence. How can I transform v into a vector where sort(unique(v)) is an actual sequence of integers? This is, transforming v into c(1,4,1,2,2,3,5,3,5) (in general, of course).
Converting v to factor and back to numeric could do the trick
as.numeric(as.factor(v))
#[1] 1 4 1 2 2 3 5 4 5
Using OP's method, we get the expected output with match
match(v, sort(unique(v)))
#[1] 1 4 1 2 2 3 5 4 5

Produce a sequence count by factor in R [duplicate]

This question already has answers here:
Create a sequential number (counter) for rows within each group of a dataframe [duplicate]
(6 answers)
Create numbered sequence for occurrences of a given nesting variable
(2 answers)
Closed 7 years ago.
I have simple dataset, one row with factors - I'd like to create a second column (SEQ) and count/sequence the factor LO column
LO SEQ
a 1
a 2
a 3
b 1
b 2
I want to count the LO factors like so...i.e. I want to create the SEQ column.
Looks so easy - but I'm stuck.
We can use getanID
library(splitstackshape)
getanID(df1, 'LO')[]
# LO .id
#1: a 1
#2: a 2
#3: a 3
#4: b 1
#5: b 2

R: Aggregate and create columns based on counts [duplicate]

This question already has answers here:
Frequency counts in R [duplicate]
(2 answers)
Closed 7 years ago.
I'm sure this question has been asked before, but I can't seem to find an answer anywhere, so I apologize if this is a duplicate.
I'm looking for R code that allows me to aggregate a variable in R, but while doing so creates new columns that count instances of levels of a factor.
For example, let's say I have the data below:
Week Var1
1 a
1 b
1 a
1 b
1 b
2 c
2 c
2 a
2 b
2 c
3 b
3 a
3 b
3 a
First, I want to aggregate by week. I'm sure this can be done with group_by in dplyr. I then need to be able to cycle through the code and create a new column each time a new level appears in Var 1. Finally, I need counts of each level of Var1 within each week. Note that I can probably figure out a way to do this manually, but I'm looking for an automated solution as I will have thousands of unique values in Var1. The result would be something like this:
Week a b c
1 2 3 0
2 1 1 3
3 2 2 0
I think from the way you worded your question, you've been looking for the wrong thing/something too complicated. It's a simple data-reshaping problem, and as such can be solved with reshape2:
library(reshape2)
#create wide dataframe (from long)
res <- dcast(Week~Var1, value.var="Var1",
fun.aggregate = length, data=data)
> res
Week a b c
1 1 2 3 0
2 2 1 1 3
3 3 2 2 0

Resources