How to add a row to a data frame in R?
Like @Khashaa and @Richard Scriven point out in comments, you have to set consistent column names for all the data frames you want to append.
Hence, you need to explicitly declare the columns names for the second data frame, de
, then use rbind()
. You only set column names for the first data frame, df
:
df<-data.frame("hi","bye")
names(df)<-c("hello","goodbye")
de<-data.frame("hola","ciao")
names(de)<-c("hello","goodbye")
newdf <- rbind(df, de)
Adding named rows to a data.frame in R
You can use rbind()
on the named vectors.
rbind(d, com = 0:3, min = 2:5)
ESL prof scope type
A 1 0 2 3
B 2 1 3 4
C 3 2 4 5
D 4 3 5 6
E 5 4 6 7
com 0 1 2 3
min 2 3 4 5
Add a row with calculations in R
Does this work:
library(dplyr)
library(stringr)
x %>%
bind_rows(x %>% summarise(a = str_c(a, lead(a), sep = '_')) %>% na.omit() %>%
bind_cols(x %>% summarise(across(2:4, ~ . - lead(.))) %>% na.omit()))
a ja fe ma
1 I 30 50 35
2 E 20 40 22
3 I_E 10 10 13
How add a row of 0 to a dataframe
We can use rbind
mat <- rbind(0, mat)
-output
mat
a b c d e
1 0 0 0 0 0
2 2 45 77 52 14
3 5 78 85 68 73
4 90 98 3 4 91
5 77 55 22 25 87
6 56 63 4 79 94
Add rows to data frame by a pattern
This may be an alternative approach:
library(tidyverse)
df1 %>%
mutate(rn = row_number()) %>%
pivot_wider(id_cols = c(ID, rn), names_from = Order, values_from = Value) %>%
mutate(post2 = if_else(!is.na(lead(post)), lead(post), pre),
pre2 = if_else(!is.na(post2) & is.na(pre), post2, pre)) %>%
select(-c(rn, pre, post)) %>%
pivot_longer(cols = c(general, pre2, post2), names_to = "Order", values_to = "Value") %>%
drop_na()
Output
# A tibble: 19 x 3
ID Order Value
<fct> <chr> <dbl>
1 1 general 1
2 1 pre2 3
3 1 post2 4
4 1 pre2 7
5 1 post2 7
6 1 pre2 0
7 1 post2 10
8 2 general 1
9 2 pre2 0
10 2 post2 0
11 2 pre2 12
12 2 post2 12
13 3 general 12
14 3 pre2 3
15 3 post2 4
16 3 pre2 6
17 3 post2 6
18 3 pre2 8
19 3 post2 8
Edit:
To generalize this solution for multiple Value
columns, you will need to first pivot_longer
to put data into a more workable format. In addition, you will want to group_by
the column name variable so that using lead
you are only looking at values appropriate for that variable.
Say for example you have two columns, Value1
and Value2
:
df1 <- data.frame("ID" = as.factor(c('1', '1', '1', '1', '1', '1', '2', '2', '2', '3', '3', '3', '3', '3')),
"Order" = as.factor(c('general', 'pre', 'post', 'post', 'pre', 'post', 'general', 'post', 'pre', 'general', 'pre', 'post', 'pre', 'pre')),
"Value1" = as.numeric(c('1', '3','4','7','0','10', '1','0','12', '12', '3', '4', '6','8')),
"Value2" = as.numeric(c('4', '2','1','9','2','15', '2','11','18', '16', '5', '5', '8','10')))
You can do the following:
df1 %>%
pivot_longer(cols = starts_with("Value"), names_to = "ValueName", values_to = "Value") %>%
mutate(rn = row_number()) %>%
pivot_wider(id_cols = c(ID, rn, ValueName), names_from = Order, values_from = Value) %>%
group_by(ID, ValueName) %>%
mutate(post2 = if_else(!is.na(lead(post)), lead(post), pre),
pre2 = if_else(!is.na(post2) & is.na(pre), post2, pre)) %>%
select(-c(rn, pre, post)) %>%
rename(pre = pre2, post = post2) %>%
pivot_longer(cols = c(general, pre, post), names_to = "Order", values_to = "Value") %>%
drop_na() %>%
arrange(ValueName, ID) %>%
print(n=50)
Output
# A tibble: 38 x 4
# Groups: ID, ValueName [6]
ID ValueName Order Value
<fct> <chr> <chr> <dbl>
1 1 Value1 general 1
2 1 Value1 pre 3
3 1 Value1 post 4
4 1 Value1 pre 7
5 1 Value1 post 7
6 1 Value1 pre 0
7 1 Value1 post 10
8 2 Value1 general 1
9 2 Value1 pre 0
10 2 Value1 post 0
11 2 Value1 pre 12
12 2 Value1 post 12
13 3 Value1 general 12
14 3 Value1 pre 3
15 3 Value1 post 4
16 3 Value1 pre 6
17 3 Value1 post 6
18 3 Value1 pre 8
19 3 Value1 post 8
20 1 Value2 general 4
21 1 Value2 pre 2
22 1 Value2 post 1
23 1 Value2 pre 9
24 1 Value2 post 9
25 1 Value2 pre 2
26 1 Value2 post 15
27 2 Value2 general 2
28 2 Value2 pre 11
29 2 Value2 post 11
30 2 Value2 pre 18
31 2 Value2 post 18
32 3 Value2 general 16
33 3 Value2 pre 5
34 3 Value2 post 5
35 3 Value2 pre 8
36 3 Value2 post 8
37 3 Value2 pre 10
38 3 Value2 post 10
The data is left in long format - but could be converted to wide as well in the end with pivot_wider
.
Adding row of one data frame at a specific spot in another dataframe in R
I hope I got the logic right for your problem ... I did it for a data.frame of 30 rows to add a row every 10 rows (as 120 is to much for a reproducable example in terms of fitting the output in the answer).
library(dplyr)
r <- 3 # your number is 46 (5520/120)
l <- 10 # your number is 120
# your long data.frame where you want to fit in ever l rows
df1 <- data.frame(dx = c("a","a","a","a","a","a","a","a","a","a",
"c","c","c","c","c","c","c","c","c","c",
"e","e","e","e","e","e","e","e","e","e"))
# your data.frame of one row to fit in every l rows
df2 <- data.frame(dy = c("X"))
# set colnames to be identical
names(df2) <- colnames(df1)
# use row number as ID and set it of as needed with the help of integer division
dff1 <- df1 %>%
dplyr::mutate(ID = dplyr::row_number()) %>%
dplyr::mutate(ID = ID + (ID-1) %/% l)
# repeat your one row df according to the quantity needed and use the row number with set off calculation
dff2 <- df2 %>%
dplyr::slice(rep(row_number(), r)) %>%
dplyr::mutate(ID = dplyr::row_number()) %>%
dplyr::mutate(ID = (ID) * l + ID)
# union both data.frames (I am supposing column types are identical!)
dff1 %>%
dplyr::union(dff2) %>%
dplyr::arrange(ID)
dx ID
1 a 1
2 a 2
3 a 3
4 a 4
5 a 5
6 a 6
7 a 7
8 a 8
9 a 9
10 a 10
11 X 11
12 c 12
13 c 13
14 c 14
15 c 15
16 c 16
17 c 17
18 c 18
19 c 19
20 c 20
21 c 21
22 X 22
23 e 23
24 e 24
25 e 25
26 e 26
27 e 27
28 e 28
29 e 29
30 e 30
31 e 31
32 e 32
33 X 33
Add a value as a new row to a dataframe but keep all other columns NA
A possible solution, based on dplyr
. We first need to convert ID
from numeric
to character
.
library(dplyr)
mydata %>%
mutate(ID = as.character(ID)) %>%
bind_rows(list(ID = "School 1"), .)
#> # A tibble: 6 × 3
#> ID subject age
#> <chr> <chr> <dbl>
#> 1 School 1 <NA> NA
#> 2 1 His 21
#> 3 2 Geo 24
#> 4 3 Geo 26
#> 5 4 His 23
#> 6 5 Geo 26
Related Topics
Put Stars on Ggplot Barplots and Boxplots - to Indicate the Level of Significance (P-Value)
Conditionally Change Panel Background With Facet_Grid
Remove an Entire Column from a Data.Frame in R
Table of Interactions - Case With Pets and Houses
Tools For Making Latex Tables in R
Change the Blank Cells to "Na"
R Spreading Multiple Columns With Tidyr
How to Merge Color, Line Style and Shape Legends in Ggplot
Subscript Letters in Ggplot Axis Label
Split Date-Time Column into Date and Time Variables
Filtering a Data Frame on a Vector
Collapsing Rows Where Some Are All Na, Others Are Disjoint With Some Nas
How to Convert Long to Wide Format With Counts
Putting Mathematical Symbols and Subscripts Mixed With Regular Letters