Trouble accessing the first column of a dataset in R - r

I am a beginner in R. I work in RStudio
After importing my dataset with the function:
mydata <- read.table("dataset.txt",sep="\t",dec=",", h=T, row.names=1)
the file is imported and I can see it in RStudio. Then, I would like to look at my data:
table (column2, column3) # it does work and give me the table
everything looks OK. However, if I ask for the First column
table (column1, column2) # it doesn't work!
I received the error message:
"Error in table(column1) : object 'ID_site' not found"
It seems the first column is not part of my dataset...
Do you know Why? Is there an option to choose before data importation?

Try removing the row.names=1 part from your read.table call. When you do that, R takes the first column of your dataset and uses it as the rownames of your data frame. That, in turn removes the ID of the column, which is why R says your "ID_site" column can't be found.

Related

Object not found in R when trying to create a table from a csv dataset

I'm using RStudio to create glm's of a csv dataset and I'm really new to R (Using it for a Uni assignment). Quick summary, it's looking at some motor claim data. I've read.csv the dataset into R;
Motor <- read.csv("motor.csv", quote="", header=TRUE)
then am trying to run
(ClaimsTab <- table(Claims))
to create a table to see the frequency of different claim amounts.
'Claims' is a header in my CSV file and there's no spelling mistakes but am returned with
Error in table(Claims) : object 'Claims' not found
I've attempted to attach a picture of my dataset.
motor dataset picture
What am I doing wrong? I imported a different file earlier and the table() function was working fine.
The name of your dataframe object is Motor.
To access a column in a dataframe in base R you do: Motor$Claims
This should work:
ClaimsTab <- table(Motor$Claims)

[TypeError]: Cannot read property 'substr' of Null

For some reason, when I run a line assigning columnn names to my dataframe (df) from another data frame (nm), I can no longer view my columns using the "$" operating; instead when I put "df$" I get the following error: Cannot read property 'substr' of Null.
Loading either dataset does not produce this problem, only when I assign column names to df using the following line:
colnames(df) = nm$Var_Code
This problem has not been happening before when running this code and is rather new. I'm not sure how to approach the problem and any assistance would be appreciated.
I am also new to R-studio, the way I get over it is to write the row names on the first row in textfile, and import data from textfile with specifying row names of data frame to be the first row of textfile.

Analyze code in dataframe with Tidycode in R

I am trying to take R code, stored in cells of the content column of a dataframe, and analyze the functions used by applying the Tidycode package. However, I first need to convert the data to a Matahari tibble before applying an unnest_calls() function.
Here is the data:
data <- read.csv("https://github.com/making-data-science-count/TidyTuesday-Analysis/raw/master/db-tmp/cleaned%20database.csv")
I have tried doing this in a number of different ways, including extracting each row (in the content column ) as an Rfile and then reading it back in with Tidycode calls, for example:
tmp<-data$content2[1])
writeLines(tmp, "tmp.R") #I've also used save() and write()
rfile<-tidycode::read_rfiles("tmp.R")
But, I keep getting errors such as: "Error in parse(text = x) : <text>:1:14: unexpected symbol
1: library(here)library"
Ultimately, what I would like to do is analyze the different types of code per file, and keep that linked with the other data in the data dataframe, such as date and username.
Any help would be greatly appreciated!

Importing a .csv with headers that show up as first row of data

I'm importing a csv file into R. I read a post here that said in order to get R to treat the first row of data as headers I needed to include the call header=TRUE.
I'm using the import function for RStudio and there is a Code Preview section in the bottom right. The default is:
library(readr)
existing_data <- read_csv("C:/Users/rruch/OneDrive/existing_data.csv")
View(existing_data)
I've tried placing header=TRUE in the following places:
read_csv(header=TRUE, "C:/Users...)
existing_data.csv", header=TRUE
after 2/existing_data.csv")
Would anyone be able to point me in the right direction?
You should use col_names instead of header. Try this:
library(readr)
existing_data <- read_csv("C:/Users/rruch/OneDrive/existing_data.csv", col_names = TRUE)
There are two different functions to read csv files (actually far more than two): read.csv from utils package and read_csv from readr package. The first one gets header argument and the second one col_names.
You could also try fread function from data.table package. It may be the fastest of all.
Good luck!
It looks like there is one variable name that is correctly identified as a variable name (notice your first column). I would guess that your first row only contains the variable "Existing Product List", and that your other variable names are actually contained in the second row. Open the file in Excel or LibreOffice Calc to confirm.
If it is indeed the case that all of the variable names you've listed (including "Existing Product List") are in the first row, then you're in the same boat as me. In my case, the first row contains all of my variables, however they appear as both variable names and the first row of observations. Turns out the encoding is messed up (which could also be your problem), so my solution was simply to remove the first row.
library(readr)
mydat = read_csv("my-file-path-&-name.csv")
mydat = mydat[-1, ]

Data Imported from Excel to R not Assigning Headers as Column Names

I'm brand new to R and am having difficulty with something very basic. I'm importing data from an excel file like this:
data1 <- read.csv(file.choose(), header=TRUE)
When I try to look at the data in the table by column, R doesn't recognize the column headers as objects. This is what it looks like
summary(Square.Feet)
Error in summary(Square.Feet) : object 'Square.Feet' not found
I need to run a regression and I'm having the same problem. Any help would be much appreciated.
Yes it recognizes, you have to tell R to select the dataframe so:
summary(data1$Square.Feet)
Where "data" is the name of your dataframe, and after the dollar goes the name of the variable
Hope it helps
UPDATE
As suggested below, you can use the following:
data1 <- read.csv(file.choose(), header=TRUE)
attach(data1)
This way, by doing "attach", you avoid to write everytime the name of the dataset, so we would go from
summary(data1$Square.Feet)
To this point after attaching the data:
summary(Square.Feet)
However I DO NOT recommend to do it, because if you load other datasets you may mess everything as it's quite common that variables have the same names, among other major problems, see here (Thanks Ben Bolker for your contribution): here , here, here and
here
if you want a summary of all data fields, then
summary(data1)
or you can use the 'with' helper function
with(data1, summary(Square.Feet))

Resources