Making 4 columns into 1 column in R [duplicate] - r

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

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

Repeating rows in data frame by using the content of a column in R [duplicate]

This question already has answers here:
Repeat each row of data.frame the number of times specified in a column
(10 answers)
Closed 2 years ago.
I want to create a data frame by repeating rows by using content of a column in a data frame. Below is the source data frame.
data.frame(c("a","b","c"), c(4,5,6), c(2,2,3)) -> df
colnames(df) <- c("sample", "measurement", "repeat")
df
sample measurement repeat
1 a 4 2
2 b 5 2
3 c 6 3
I want to repeat the rows by using the "repeat" column and its content to get a data frame like the one below. Ideally, I would like to have a function to this.
sample measurement repeat
1 a 4 2
2 a 4 2
3 b 5 2
4 b 5 2
5 c 6 3
6 c 6 3
7 c 6 3
Thanks in advance!
Solved. df[rep(rownames(df), df$repeat), ] did the job.

Tallying values in single column and separating into Rows in R [duplicate]

This question already has answers here:
Counting the number of elements with the values of x in a vector
(20 answers)
Closed 6 years ago.
I have a single row of numbers. I'm wondering how I can separate it out so that it outputs columns that total the tally of each set of numbers. I've tried playing around with "separate" but I can't figure out how to make it work.
Here's my data frame:
2
2
2
2
2
4
4
4
I'd like it to be
2 4
5 3
You can use the table() function.
> df
V1
1 2
2 2
3 2
4 2
5 2
6 4
7 4
8 4
> table(df$V1)
2 4
5 3
We can use tabulate which would be faster
tabulate(factor(df1$V1))
#[1] 5 3

How to make a list of all column-values from multiple columns in R? [duplicate]

This question already has answers here:
Using R convert data.frame to simple vector
(4 answers)
Closed 7 years ago.
Consider the following data frame:
> df
a b
1 1 4
2 2 5
3 3 6
I want to aggregate a list of all values from my data frame, like this:
> c(df$a,df$b)
[1] 1 2 3 4 5 6
In my real data frame there are a lot of columns, so i need to select them by specifying a range. Naming every column would be impractical. I tried it like this:
> c(df[1:2])
$a
[1] 1 2 3
$b
[1] 4 5 6
The result is a list of columns, not a list of all column values. How can this be achieved?
The ultimate goal is to make a frequency distribution of all values (the set of values is finite).
you probably want:
unlist(df)
a1 a2 a3 b1 b2 b3
1 2 3 4 5 6
or with matrix:
c(as.matrix(df))

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

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)

Resources