Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Using the print() Function | Basic Syntax and Operations
R Introduction: Part I
course content

Kursinnhold

R Introduction: Part I

R Introduction: Part I

1. Basic Syntax and Operations
2. Basic Data Types and Vectors
3. Factors

book
Using the print() Function

After running the code in the previous chapter, you may have noticed [1] preceding the results. This is R's way of indicating that the line starts with the first element in your output. As you progress through the course, you'll encounter outputs with multiple elements.

Let's say we want to carry out a division operation:

1
5/3
copy

As we already know, in R, it's unnecessary to use a specific function to display output values. However, if your goal is to round a number to a desired number of significant figures, you can use the print() function, which you may also recognize from other programming languages.

Here's how you use the function:

r

For instance, we can round the division result from our previous example to three significant figures:

1
print(5/3, digits = 3)
copy

As demonstrated, the output of the expression has been rounded to three significant figures.

Oppgave

Swipe to start coding

  1. On the first line, execute a division of 50 by 7.
  2. On the second line, divide 50 by 7 again, but this time round the result to four significant figures using the print() function.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 3
toggle bottom row

book
Using the print() Function

After running the code in the previous chapter, you may have noticed [1] preceding the results. This is R's way of indicating that the line starts with the first element in your output. As you progress through the course, you'll encounter outputs with multiple elements.

Let's say we want to carry out a division operation:

1
5/3
copy

As we already know, in R, it's unnecessary to use a specific function to display output values. However, if your goal is to round a number to a desired number of significant figures, you can use the print() function, which you may also recognize from other programming languages.

Here's how you use the function:

r

For instance, we can round the division result from our previous example to three significant figures:

1
print(5/3, digits = 3)
copy

As demonstrated, the output of the expression has been rounded to three significant figures.

Oppgave

Swipe to start coding

  1. On the first line, execute a division of 50 by 7.
  2. On the second line, divide 50 by 7 again, but this time round the result to four significant figures using the print() function.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 3
Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Vi beklager at noe gikk galt. Hva skjedde?
some-alt