How to add value to column when data frame is empty in r

Kush Patel

I have data frame that I have to initialized as empty data frame. Now I have only column available, I want to add it to empty data frame. How I can do it? I will not sure what will be length of column in advance.

Example

df = data.frame(a= NA, b = NA, col1= NA)
....
nrow(col1) # Here I will know length of column, and I only have one column available here.
df$col1 <- col1

error is as follows:

Error in `$<-.data.frame`(`*tmp*`, "a", value = c("1",  : 

replacement has 5 rows, data has 1

Any help will be greatful

SymbolixAU

use cbind

df = data.frame(a= NA, b = NA)

col1 <- c(1,2,3,4,5)
df <- cbind(df, col1)

#    a  b col1
# 1 NA NA    1
# 2 NA NA    2
# 3 NA NA    3
# 4 NA NA    4
# 5 NA NA    5

After your edits, you can still use cbind, but you'll need to drop the existing column first (or handle the duplicate columns after the cbind)

cbind(df[, 1:2], col1)

## or if you don't know the column indeces
## cbind(df[, !names(df) %in% c("col1")], col1)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Function to add empty colum in data frame when column of another df is different in R

How to add data from column in a data frame to a corpus based on a value from another column in R?

R - How can I add an empty POSIXct column to a data.frame / tibble which already exists?

How to create empty data frame with column names specified in R?

How do I initialize an empty data frame *with a Date column* in R?

Add a unique identifier to the same column value in R data frame

add rows to data frame for each value from a character column R

Add a column to empty data.frame

Add a column into the data frame when using lapply in R

When selecting row based on column value got empty row but column value is available in data frame

How to reshape data frame, header value to column value in R

How to add column to a data frame?

In R: Replacing value of a data frame column by the value of another data frame when between condition is matched

How to add a name column of character vectors to a data frame in R

R - How to add a timestamp column to a data frame definition

How to add a new column in data frame using calculation in R?

How to add a column of different length to a data frame in R?

How to add a column with sequential values that expands a data frame in R

How to add parentheses to column values in a data frame in R?

How to change date format in R data frame and add to new column?

How to find the highest value of a column in a data frame in R?

How to edit each text value of a column of a data frame in r?

in R how to apply a value of a column to multiple columns in the same data frame

Error when subsetting in R - empty data frame

How to add new column in R data frame showing sum of a value in a current row and a prior row, if certain conditions are met in the 2 rows?

How to assign a value to a column based on value in other column in R data frame or data table

Add column to aggregate from data frame in R

Aggregate by group AND add column to data frame in R

Using a variable to add a data frame column in R