I am changing from holding data inside Rstudio to instead keeping it in SQL and importing tables as I need them.
The issue I keep running into is that when I import the table from SQL, I have a data frame that now needs to be converted to XTS, but the index is now an auto integer and not the first column which contains what should be my index.
Is there a way to just specify a new column as the index?
I assume there has to be but I can't find anything.
Data
Figured this out rather quickly after posting the question.
#Specified the row name as the index using rownames()
rownames(temp_from_sql) <- temp_from_sql$`Date Time`
#deleted the 'Date Time' column
temp <- temp_from_sql[,-1]
Related
I am trying to convert an Oracle database table into an "R" data frame.
I am using the dplyr::tbl function as well as dbplyr::in_schmema to connect to the specific schema and table within the Oracle database.
Table <- dplyr::tbl(my_oracle, dbplyr::in_schema('SCHEMA_NAME', 'TABLE_NAME'))
This is the part that confuses me as the result is an object called "Table" that is a "List of 2" as seen below. The two items within the list are also lists of two.
I am able to convert this to a data frame by wrapping it with as.data.frame like this:
Table2 <- as.dataframe(dplyr::tbl(my_oracle, dbplyr::in_schema('SCHEMA_NAME', 'TABLE_NAME')))
However, when I do this i takes a very long time (hours for some tables) to convert to a data frame. I am wondering if there is a more efficient way to achieve the outcome of converting the Oracle table into a usable data frame?
Also any insight into why dplyr::tbl results in a list of lists would also be very appreciated.
Thanks in advance.
I am uploading an R data frame to Big Query with this:
bq_table_upload("project.dataset.table_name", df_name)
It works, but the order of the columns is different in BQ than in R.
Is it possible to get the table to inherit the order of the columns from the data frame? I have looked through the documentation and have not found this option, though it is certainly possible I missed it.
As pointed out in the comments by Oluwafemi Sule, it's only a matter of providing the dataframe in the "fields" attribute like below:
bq_table_upload("project.dataset.table_name", values = df_name, fields=df_name)
I am building a function that requires me to rename columns in a data frame, where the original column names are stored in a variable from user input.
#self$options$DV is a string identifying the column of interest, such as 'chosenvarname':
DVlabel <- self$options$DV
However, the dplyr::rename function doesn't work when using this variable or its symbolic link:
df1 <-plyr::rename(df1,c(DVlabel='DV'))
df1 <-plyr::rename(df1,c(self$options$DV='DV'))
Even when DVlabel is set to equal a valid column name in the data frame, it still doesn't work
It only works properly when using the actual column name, which makes me think that this function doesn't work with symbolic links:
df1 <-plyr::rename(df1,c(OriginalColumnName='DV'))
Is there another way to use the column name identified in self$options$DV as the basis for renaming that same column to something else?
Put differently, is there any way to rename a column using symbolic links to the column name that don't otherwise exist in the data?
Alternatively, is there some way to construct a column reference, such as data$var1, where the "var1" component is extracted from some other variable (e.g., DVlabel or self$options$DV?)
I found a way around this by using the following to rename the columns:
colnames(df1)[colnames(df1) == self$options$DV] <- 'DV'
i m creating a risk dashboard , the problem is that i need the data set to be reactive , i have a simple dataset composed of countries (8) , sectors and values , what i want is that my app will be able to deal with different data sets for example if we change the colnames (country becomes pays) and we change the position of the col ,the app will recognize the column as country (in reality the data set is composed of an undefined number of variables with unkown names)
for example for the country column , i thought of creating a list that contains all country names and and when the first row of a column matches with a country from that list ,the column become names country
like that the problem is solved for one variable and what about the other ones
I think that's unnecesary complexity.
I suggest you to build an script to clean your data first with those specifications and then use it as a source.
You can use pattern recognition to match columns but be sure there aren't similar columns, for example, if you have two numerical variables there's a big problem.
Via Shiny I suggest you:
fileInput to import your database
Visualizate your database using DT
Create as many textInput boxes as columns you have
Manually change colnames using dplyr::rename and the boxes
Use the transformed database in your dashboard
Other options can be made using base::grep and dplyr.
This is probably a basic question, but why does R think my vector, which has a bunch of words in it, are numbers when I try to use these vectors as column names?
I imported a data set and it turns out the first row of data are the column headers that I want. The column headers that came with the data set are wrong ones. So I want to replace the column names. I figured this should be easy.
So what I did was I extracted the first row of data into a new object:
names <- data[1,]
Then I deleted the first row of data:
data <- data[-1,]
Then I tried to rename the column headers with the "names" object:
colnames(data) <- names
However, when I do this, instead of changing my column names to the words within the names object, it turns it into a bunch of numbers. I have no idea where these numbers come from.
Thanks
You need to actually show us the data, and the read.csv()/read.table() command you used to import.
If R thinks your numeric column is string, it sounds like that's because it wrongly includes the column name, i.e. you omitted header=TRUE in your read.csv()/read.table() import.
But show us your actual data and commands used.