Pipe in R Studio

data("ToothGrowth")
View(ToothGrowth)

#Nested

arrange(filter(x,dose==0.5),len)
pipe

x <- ToothGrowth %>%
filter(dose==0.5) %>%
arrange(len)

When I use the Nested code its runs fine. ToothGrow data arrange and sort according to the code.
But when I add the pipe code. It gives the output in the console

x <- ToothGrowth %>%

  • filter(dose==0.5) %>%
  • arrange(len)

Why ?Please help me

They are equivalent.

The only difference in your code is that you've assigned the piped example but not assigned the nested one.

I also assume here...

... you'll be using "ToothGrowth" rather than "x"?

See the code below which demonstrates that these are equivalent:

library(tidyverse)

# Nested

nested <- arrange(filter(ToothGrowth, dose == 0.5), len)

# Pipe

piped <- ToothGrowth %>%
  filter(dose == 0.5) %>%
  arrange(len)

# Compare

nested == piped
#>        len supp dose
#>  [1,] TRUE TRUE TRUE
#>  [2,] TRUE TRUE TRUE
#>  [3,] TRUE TRUE TRUE
#>  [4,] TRUE TRUE TRUE
#>  [5,] TRUE TRUE TRUE
#>  [6,] TRUE TRUE TRUE
#>  [7,] TRUE TRUE TRUE
#>  [8,] TRUE TRUE TRUE
#>  [9,] TRUE TRUE TRUE
#> [10,] TRUE TRUE TRUE
#> [11,] TRUE TRUE TRUE
#> [12,] TRUE TRUE TRUE
#> [13,] TRUE TRUE TRUE
#> [14,] TRUE TRUE TRUE
#> [15,] TRUE TRUE TRUE
#> [16,] TRUE TRUE TRUE
#> [17,] TRUE TRUE TRUE
#> [18,] TRUE TRUE TRUE
#> [19,] TRUE TRUE TRUE
#> [20,] TRUE TRUE TRUE

Created on 2022-03-05 by the reprex package (v2.0.1)

pipe

x <- ToothGrowth %>%
filter(dose==0.5) %>%
arrange(len)

My this program is not working.

Output in the console is like this

x <- ToothGrowth %>%

  • filter(dose==0.5) %>%
  • arrange(len)

The normal result of running this code

x <- ToothGrowth %>%
  filter(dose==0.5) %>%
  arrange(len)

is that the console will show

> x <- ToothGrowth %>%
+   filter(dose==0.5) %>%
+   arrange(len)

If you want to see what x is, you can just type x in the console, like this

> x
    len supp dose
1   4.2   VC  0.5
2   5.2   VC  0.5
3   5.8   VC  0.5
4   6.4   VC  0.5
5   7.0   VC  0.5
6   7.3   VC  0.5
7   8.2   OJ  0.5
8   9.4   OJ  0.5
9   9.7   OJ  0.5
10  9.7   OJ  0.5
11 10.0   VC  0.5
12 10.0   OJ  0.5
13 11.2   VC  0.5
14 11.2   VC  0.5
15 11.5   VC  0.5
16 14.5   OJ  0.5
17 15.2   OJ  0.5
18 16.5   OJ  0.5
19 17.6   OJ  0.5
20 21.5   OJ  0.5

Check the Environment tab in RStudio to see if x is there.

Your code assigned the output to x as a new data frame, and it would not automatically print out the content in the console.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.