Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

A new Data Days event is coming soon! This time we’re going bigger than ever. Fabric, Power BI, SQL, AI and more. Don't miss out.

Reply
Gracie
Helper II
Helper II

How to put x as dates for R linear regression

I am writing R script to do linear regression. My dateset is date and price. Date is from Date table and Price is from Sales table. These two tables have no relationship setup. How can I put date as x axis ? And how to cancel #Remove duplicated rows because I don't want to remove duplicated rows?

 

Here is my code:

#Create dataframe
#dataset <- data.frame(Date,Price)

#Remove duplicated rows
#dataset <- unique(dataset)

strDates <- dataset[,1] x <- as.Date(strDates, "%m/%d/%Y") y <- as.numeric(dataset[,2]) plot(y~x, xlim = c(as.POSIXct("1/1/2010", format = "%m/%d/%Y"),as.POSIXct("1/1/2010", format = "%m/%d/%Y"))) lm.ab<-lm(y ~ x) abline(lm.ab) summary(lm.ab)
3 REPLIES 3
v-huizhn-msft
Microsoft Employee
Microsoft Employee

Hi @Gracie,

Please create a index column in Date and Sales table separately.  Then merge the two table to one according to index field.

Then you load the Index, Date and Price to create a dataset.

Change your corresponding part of code to the following code. 

strDates <- dataset[,3]
y <- as.numeric(dataset[,3])

Then run your R code and change if it works fine.

Best Regards,
Angelia

Hi @v-huizhn-msft

 

It doesn't work. I guess your point is to add the index to avoid removing duplicated rows. But the x-axis still can't show as dates.

 

 

Hi @Gracie,

>>But the x-axis still can't show as dates.

Could you please share a screenshot for further analysis?

Thanks,
Angelia

Helpful resources

Announcements
May Power BI Update Carousel

Power BI Monthly Update - May 2026

Check out the May 2026 Power BI update to learn about new features.

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.