R, dataframe manipulation, sort [duplicate] - r

This question already has answers here:
How to join (merge) data frames (inner, outer, left, right)
(13 answers)
Closed 2 years ago.
I have the following two DFs, each with two columns (stringIDs, and counts). Data looks like:
I'd like to transform this to 1 DF, sorted A to Z, (with all stringIDs from both DFs, counts from DF1, counts from DF2). If the stringID does not exist, the corresponding count should be 0. Is there a package in R that will allow me to do this transformation?
I have:
I'd like the data transformed to:

Try this. It is a merge task:
#Data
df1 <- data.frame(stringid=paste0('string',1:4),counts=c(10,11,11,13),stringsAsFactors = F)
df2 <- data.frame(stringid=paste0('string',c(1,3:5)),counts=c(10,11,11,10),stringsAsFactors = F)
#Merge
dfmerged <- merge(df1,df2,by='stringid',all=T,suffixes = c('_df1','_df2'))
dfmerged[is.na(dfmerged)]<-0
stringid counts_df1 counts_df2
1 string1 10 10
2 string2 11 0
3 string3 11 11
4 string4 13 11
5 string5 0 10

Related

R - Merge data from two data frames with different nrow if text condition is met [duplicate]

This question already has answers here:
How to join (merge) data frames (inner, outer, left, right)
(13 answers)
Closed 1 year ago.
id1=c('1text','2text','3text')
df1=data.frame(id1)
df1
id1
1 1text
2 2text
3 3text
id2=c('2text','3text')
area2=c(11,22)
df2=data.frame(id2,area2)
df2
id2 area2
1 2text 11
2 3text 22
3 1text 33
I would like to add the data of area2 to the data frame df1 in those rows, where id1=id2, so i have a new column in df1 with the area. It should look like this:
id1 area1
1 1text NA
2 2text 11
3 3text 22
Can somebody help?
A solution with dplyr:
library(dplyr)
id1=c('1text','2text','3text')
df1=data.frame(id1)
id2=c('2text','3text','1text')
area2=c(11,22,33)
df2=data.frame(id1=id2,area2)
inner_join(df1,df2,by="id1")

Reshaping dataframe to list values over unique id - back and forth [duplicate]

This question already has answers here:
Collapse text by group in data frame [duplicate]
(2 answers)
Collapse / concatenate / aggregate a column to a single comma separated string within each group
(6 answers)
Closed 3 years ago.
I want to condense information in a dataframe to reduce the number of rows.
Consider the dataframe:
df <- data.frame(id=c("A","A","A","B","B","C","C","C"),b=c(4,5,6,1,2,7,8,9))
df
id b
1 A 4
2 A 5
3 A 6
4 B 1
5 B 2
6 C 7
7 C 8
8 C 9
I want to collapse the dataframe to all unique values of "id" and list the values in variable b. The result should look like
df.results <- data.frame(id=c("A","B","C"),b=c("4,5,6","1,2","7,8,9"))
df.results
id b
1 A 4,5,6
2 B 1,2
3 C 7,8,9
A solution for the first step is:
library(dplyr)
df.results <- df %>%
group_by(id) %>%
summarise(b = toString(b)) %>%
ungroup()
How would you turn df.results back into df?

Combining multiple columns in one R [duplicate]

This question already has answers here:
Flatting a dataframe with all values of a column into one
(3 answers)
Closed 5 years ago.
How can I combine multiple all dataframe's columns in just 1 column? , in an efficient way... I mean not using the column names to do it, using dplyr or tidyr on R, cause I have too much columns (10.000+)
For example, converting this data frame
> Multiple_dataframe
a b c
1 4 7
2 5 8
3 6 9
to
> Uni_dataframe
d
1
2
3
4
5
6
7
8
9
I looked around Stack Overflow but without success.
We can use unlist
Uni_dataframe <- data.frame(d = unlist( Multiple_dataframe, use.names = FALSE))
Or using dplyr/tidyr (as the question is specific about it)
library(tidyverse)
Uni_dataframe <- gather(Multiple_dataframe, key, d) %>%
select(-key)

How to merge two dataframes R [duplicate]

This question already has answers here:
How to join (merge) data frames (inner, outer, left, right)
(13 answers)
Closed 6 years ago.
I have two data frames with some overlapping variables and some not. Each variable has an attribute (frequency of variable) and I need to combine the two into one dataframe where the result is two columns of attributes, one corresponding to the first dataframe, and the second corresponding to the first data frame, and the union of all the variables are represented.
dataframe 1:
var frequency
a 3
b 2
d 5
dataframe 2:
var frequency
a 2
b 3
c 3
Resulting dataframe:
var frequency1 frequency2
a 3 2
b 2 3
c 0 3
d 5 0
Thanks for your help.
This seems to work for me:
df1 = read.csv('df1.csv')
df2 = read.csv('df2.csv')
df1$frequency1 = df1$frequency
df2$frequency2 = df2$frequency
df1$frequency = NULL
df2$frequency = NULL
df = merge(df1, df2, by = 'var', all = TRUE)
print(df)
The idea is that if you want frequency1 and frequency2 to be the names in the final merged dataframe, you can rename them in df1 and df2 before merging. This produces:
var frequency1 frequency2
1 a 3 2
2 b 2 3
3 d 5 NA
4 c NA 3

How to associate the values of a column to another column of a different data frame [duplicate]

This question already has answers here:
How to join (merge) data frames (inner, outer, left, right)
(13 answers)
Closed 6 years ago.
I have two different data frame, in one of them I have the information id , in the other I have the id and a vector n, I would like associate the values of n to id in the first dataframe.
for exemple:
df1 <-data.frame(
id = c(1,1,1,2,2,3,3,3,3)
)
df2 <- data.frame(
id = c(1,2,3),
n = c(5,9,8)
)
I would like as output:
df1:
id n
1 5
1 5
1 5
2 9
2 9
3 8
3 8
3 8
3 8
df1 <- merge(df1, df2, by = c("id") )

Resources