Home > Mobile >  (FIXED) Error while creating data frame - unexpected input
(FIXED) Error while creating data frame - unexpected input

Time:10-11

R studio sends an error message when I try creating a data frame. My understanding of R is very basic, as far as I know the error message means that I typed something wrong. I've tried using other functions for creating the data frame and nothing changes, I've copied and pasted it, I've typed it bit by bit, I've restarted R. I've asked on my university forum, but we haven't found an issue yet. I really can't find the error.

This is the error message that it gives me:

Error: unexpected input in "sdg_data <- data.frame (Industry = c(""

EDIT: this is the full code with the changes proposed by Pau and after a bit of tweaking the code works.

sdg_data <- data.frame(Industry = c("Financial Services", "Non Profit and Services", "Energy", "Real Estate", "Food and Beverage Products", "Chemicals", "Forest and Paper Products", "Logistics", "Mining", "Tourism and Leisure"),

  •                    Number_of_Companies = c(34, 16, 11, 10, 9, 7, 1, 3, 7, NA), 
    
  •                    Over_Three_Stars_Awarded = c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, FALSE), 
    
  •                    Most_Common_Goals = c("G8", "G12", "G13", "G5", "G1", "G10", "G9", "G7", "G17", "G3"), 
    
  •                    stringsAsFactors = FALSE)
    

CodePudding user response:

you cant use a whitespace between the function call and the following parentheses:

# wont work:
x <- data.frame (mtcars)
# works:
x <- data.frame(mtcars)

CodePudding user response:

There a some errors in the code:

  1. The quotes are not recognized by R, hence the error.
sdg_data <- data.frame(Industry=c(“Financial Services”),stringsAsFactors = FALSE) #ERROR
sdg_data <- data.frame(Industry=c("Financial Services"),stringsAsFactors = FALSE) #WORKS
  1. Including spaces in the name of variables is problematic. Often is useful to avoid them or use a "_" instead:
sdg_data <- data.frame(Number of Companies = c(34, 21, 16, 11, 10, 9, 7, 1, 3, 7, NA), stringsAsFactors = FALSE)
Error: unexpected symbol in "sdg_data <- data.frame(Number of"
  1. There is also a comma right before a parenthesis right at the end of the code that should be placed after the parenthesis.
  • Related