R: Insert a Vector as a Row in Data.Frame

R: Insert a vector as a row in data.frame

I wouldn't claim this to be the most elegant and pretty solution out there, but it gets the job done.
Notice that each dataframe row carries its own row name, which becomes a problem when inserting new lines. That being said, you can mend this with row.names (see below).

my.df <- data.frame(a = runif(10), b = runif(10), c = runif(10))
my.vec <- c(1, 1, 1)
new.df <- rbind(my.df[1:5, ], my.vec, my.df[6:nrow(my.df), ])
new.df
a b c
1 0.45433791 0.3798105 0.84514864
2 0.07074529 0.4985765 0.53912585
3 0.09645574 0.5441647 0.96636213
4 0.60788436 0.6070706 0.53791603
5 0.01593911 0.1697248 0.62697924
6 1.00000000 1.0000000 1.00000000
61 0.98455694 0.2206702 0.85500531
7 0.85356834 0.5279596 0.27462326
8 0.48028935 0.6689572 0.05428349
9 0.95675901 0.6875491 0.77642924
10 0.24691330 0.7980741 0.24013096

row.names(new.df) <- 1:nrow(new.df) # make row names pretty again

Add a row in a dataframe taking values from a vector R

You may add a list column to the dataframe and then use tidyr::unnest to get them as separate rows.

inds <- df$w2 == "eat"
df$new_col[!inds] <- 'no correspondance'
df$new_col[inds] <- list(vec)
tidyr::unnest(df, new_col)

# id w1 w2 new_col
# <dbl> <chr> <chr> <chr>
#1 123 abc eat value1
#2 123 abc eat value2
#3 123 fgh drink no correspondance
#4 456 kit ty no correspondance

Insert Values of Vector into Data Frame on Nearest Value

Use findInterval() to identify valid indexes

aidx = findInterval(c, df$a)
bidx = findInterval(c, df$b) + 1
keep = aidx == bidx

then update the original data frame

df[aidx[keep], "c"] = c[keep]

I would expect this to be fast for up to 100's of millions of rows, provided the data.frame rows are already sorted.

How to add a named vector as a row to a data frame, reordered according to column name order?

Make a data frame out of v2 prior to the rbind:

rbind(df, as.data.frame(t(v2)))
## id va vb vc
## 1 1 11 21 31
## 2 2 12 22 32
## 3 4 14 25 NA
## 4 9 19 NA 34

Here is why this works:

v2 has names, but it acts like a column vector to as.data.frame:

as.data.frame(v2)
## v2
## va 19
## id 9
## vc 34
## vb NA

Thus, you must transpose the data to put it into the correct form:

as.data.frame(t(v2))
## va id vc vb
## 1 19 9 34 NA

How to create a one-row data frame from a vector in R?

We could use unnest_wider after returning the output in a list in summarise

library(dplyr)
library(tidyr)
mtcars %>%
group_by(cyl) %>%
summarise(out = list(boxplot.stats(wt)$stats)) %>%
unnest_wider(out) %>%
rename_at(-1, ~ str_replace(., '\\.+', 'x'))
# A tibble: 3 x 6
# cyl x1 x2 x3 x4 x5
# <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 4 1.51 1.88 2.2 2.62 3.19
#2 6 2.62 2.82 3.22 3.44 3.46
#3 8 3.17 3.52 3.76 4.07 4.07

Or if we want to use the OP's method, then set the names for that vector and use as_tibble_row

library(purrr)
library(stringr)
mtcars %>%
group_by(cyl) %>%
group_map(~ tibble(cyl = first(.x$cyl),
setNames(boxplot.stats(.$wt)$stats, str_c('x', 1:5)) %>%
as_tibble_row) , .keep = TRUE) %>%
bind_rows
# A tibble: 3 x 6
# cyl x1 x2 x3 x4 x5
# <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 4 1.51 1.88 2.2 2.62 3.19
#2 6 2.62 2.82 3.22 3.44 3.46
#3 8 3.17 3.52 3.76 4.07 4.07

As the output of group_map is always a list, it may be better to use group_modify to return a tbl thus avoiding the last map_dfr/bind_rows

mtcars %>% 
group_by(cyl) %>%
group_modify(~ setNames(boxplot.stats(.$wt)$stats, str_c('x', 1:5)) %>%
as_tibble_row , .keep = TRUE) %>%
ungroup
# A tibble: 3 x 6
# cyl x1 x2 x3 x4 x5
# <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 4 1.51 1.88 2.2 2.62 3.19
#2 6 2.62 2.82 3.22 3.44 3.46
#3 8 3.17 3.52 3.76 4.07 4.07

Add vector to each row of a dataframe

There's a specific function for this purpose:

sweep(data, 2, to_add, "+")
X0 X3 X6 X9 X12 X18 X21
chr5_89951600_89954799 24 26 23 23 34 33 29
chr16_70874600_70876999 21 26 29 15 23 26 22
chr2_51953000_51955199 13 26 11 15 31 20 39
chr3_143120600_143123799 31 40 39 24 30 31 37
chr15_34771400_34774599 33 42 35 22 29 18 40
chr2_13077000_13083999 42 73 56 49 78 57 59

It applies a particular function across all items of a "margin" using the values in the "statistic". It's the mean of that margin (and here you want columns) by default, but it can be any vector of the same length as the margin.

R: add character vector to data.frame row

Suppose the initial dataset ('DF') have some 'factor' columns as in the post. Before rbind the vector ('v'), change the 'factor' columns to 'character'. For that create a logical index ('indx') of the factor columns, loop through the factor columns and convert to character (lapply(DF[indx],..)). rbind the vector with the dataset and convert the column classes with type.convert.

indx <- sapply(DF, is.factor)
DF[indx] <- lapply(DF[indx], as.character)
DF1 <- rbind(v, DF)
DF1[] <- lapply(DF1, type.convert)
DF1
# V1 V2 V3 V4
#1 g H 7 8
#2 a e 1 4
#3 d f 2 5
#4 c g 3 6

str(DF1)
#'data.frame': 4 obs. of 4 variables:
# $ V1: Factor w/ 4 levels "a","c","d","g": 4 1 3 2
# $ V2: Factor w/ 4 levels "e","f","g","H": 4 1 2 3
# $ V3: int 7 1 2 3
# $ V4: int 8 4 5 6

data

DF <- structure(list(V1 = structure(c(1L, 3L, 2L), .Label = c("a", 
"c", "d"), class = "factor"), V2 = structure(1:3, .Label = c("e",
"f", "g"), class = "factor"), V3 = 1:3, V4 = 4:6), .Names = c("V1",
"V2", "V3", "V4"), class = "data.frame", row.names = c(NA, -3L))

v <- c('g', 'H', 7, 8)

Add vector as a column to a data.frame with fill = NA

We create a NA column and then assign the 'my_vector' based on the length of the vector. Here seq_along(my_vector) return 1:3, thus the first 3 elements are replaced with 'my_vector' values

my_df$new_column <- NA_character_
my_df$new_column[seq_along(my_vector)] <- my_vector

Or this can be done in a single step if we pad NA at the end by making use of length<-

my_df$new_column <-  `length<-`(my_vector, nrow(my_df))

-output

my_df
# person role new_column
#1 Oleg EDITOR-IN-CHIEF Lomonosov University
#2 Yurii DEPUTY EDITORS-IN-CHIEF Russian Academy of Sciences
#3 Igor DEPUTY EDITORS-IN-CHIEF Institute of Acoustics, Moscow, Russia
#4 Mikhail Coordinating Editor <NA>


Related Topics



Leave a reply



Submit