How to insert user data into a pre-existing data.frame ?

I'm trying rbind but it's not giving any output and is not allowing me to create data.frame, what should I try then?

Please show a little data from each data frame and the code you are using to combine them. If your data frame is named DF, you can provide a sample of your data by posting the output of

dput(head(DF))
create <- function(n)
{
  de <- data.frame(Name = c(''), Makrs = c(''))
  for (i in 1:n) {
    name <- readline(prompt = paste("Enter ",i," Name : "))
    marks <- as.numeric(readline(prompt = paste("Enter Marks of ",name," : ")))
    if (nchar(marks) == 2){
      de <- rbind2(de, data.frame(Name = c(name), Marks = c(marks)))
    }
    else{
      repeat create(n)
    }
  }
}

a <- as.numeric(readline(prompt = "Enter the number of enteries you want to make : "))
create(a)

Hi @Arnav1827,

It looks like you misspelled "Marks" when you initiate your de data frame. You spelled it "Makrs". There is your first problem.

Next is when you initiate Marks as an empty string, but then ask the user for it and specify it as a numeric. I would just initiate both of your variables as NA. Also this may be your intention, but if you define your dataframe in the function, each time a user enters information it will reinitiate the dataframe with no data. Which may be your intention, but if you want to be able to call the function one day, enter some data, then call the function again on another day, enter more data, then look back at all the data you've entered over the last couple days, your current setup will not allow that. If you want to reference any data you've entered over time, then you'll want to initiate your dataframe before you define your create function.

Then you'll want to return the de dataframe at the end of the function as well. This will push the data that your user has been entering into the dataframe. However to do that, when you call the function, you'll want to call it as defining the de dataframe. I got a little lost in my words here, so here's the modified code that you provided, with the edits I've suggested:

# Initiate "de" here to save entries from multiple instances.
de <- data.frame(Name = NA, Marks = NA)

create <- function(n)
{
# Or initiate "de" here if you want to clear the entered data after each instance
  de <- data.frame(Name = NA, Marks = NA) # fixed spelling of "Marks"
  for (i in 1:n) {
    name <- readline(prompt = paste("Enter ",i," Name : "))
    marks <- as.numeric(readline(prompt = paste("Enter Marks of ",name," : ")))
    if (nchar(marks) == 2){
      de <- rbind2(de, tibble(Name = c(name), Marks = c(marks)))
    }
    else{
      repeat create(n)
    }
  }
  return(de) # return "de" to pass entered information
}

a <- as.numeric(readline(prompt = "Enter the number of enteries you want to make : "))
de <- create(a) # pass the create() function to a dataframe to save it

head(de) # check out your entry

Hi @jonesey441 thanks for your support, it worked.
But I'm not getting why we use tibble, and also it created the first row as NA, NA.

output:

> head(de)
  Name Marks
1 <NA>    NA
2   dt    54
3  xuy    45

Also, please help with importing the data frame, as like I want to take the data frame name from user and then add more data into it according to the number of attributes it is having, I'm not finding any exact solution for that.

Hi @Arnav1827 ,

I think I was playing around with using tibble instead of data.frame in case there were issues with that. You can use data.frame and it should work fine.

The reason you're getting NA as the first row is because of the way we set up the dataframe in the first place, and then use rbind2 to add data to it, without overwriting the pre-exisiting data (i.e the NAs).

If your user is bringing in an already existing dataframe then you won't need to declare the NAs. Or if you'd like you can filter out any row where both Name and Marks is NA. I added a filter function before returning de:

de <- filter(de, Name != "NA" & Marks != "")
  return(de) # return "de" to pass entered information

I think that solves the original question.

But now you want to take an existing dataframe, supplied by the user, and add rows to each already existing column? Or add columns to the same number of already existing rows?

Hi @jonesey441, It worked correctly,

Yes, this is the actual question I'm working on and the above query was just a part of it, I apologize for that as I was not clear about what I'm supposed to ask.

Hi @Arnav1827 ,

I see, well maybe for clarities sake, we close this topic since the question that was posed in the original post was answered, and you start a new thread with the more direct version of your question?