-1

I want to separate a column into two columns.

When I add the code below the console says the column was split.

> separate(refine, "Product code / number", into =  c("product code", "number"), sep = "-")
# A tibble: 25 x 9
   company `product code` number             address   city         country            
   name
 *   <chr>          <chr>  <chr>               <chr>  <chr>           <chr>           
<chr>
1 philips              p      5 Groningensingel 147 arnhem the netherlands   
dhr p. jansen
2 philips              p     43 Groningensingel 148 arnhem the netherlands   
dhr p. hansen
3 philips              x      3 Groningensingel 149 arnhem the netherlands   
dhr j. Gansen
4 philips              x     34 Groningensingel 150 arnhem the netherlands   
dhr p. mansen
5 philips              x     12 Groningensingel 151 arnhem the netherlands  
dhr p. fransen
6 philips              p     23 Groningensingel 152 arnhem the netherlands 
dhr p. franssen

The Problem is when I check the result the column wasn't split.

refine[,2]
# A tibble: 25 x 1
   `Product code / number`
                 <chr>
 1                     p-5
 2                    p-43
 3                     x-3
 4                    x-34
Hadsga
  • 185
  • 2
  • 4
  • 15

2 Answers2

0

You will never see the output in the script section of rstudio... Or in an .R script file, for that matter. The script only stores the commands. Output can be seen in the console, and it can be stored as objects in the environment. In rstudio you can visualize the environment and some types of output through the command View(). Take a look at rstudio's cheat sheet: Rstudio.pdf

Nicolás Velasquez
  • 5,623
  • 11
  • 22
0

You are not assigning the result. separate doesn’t modify the existing variable refine (few functions in R ever modify their arguments). It returns a new table with the split columns. You need to assign the result to a new (or existing) name:

result = separate(refine, "Product code / number", into =  c("product code", "number"), sep = "-")

Instead of creating a new variable (result), you can also overwrite refine (refine = separate(refine, …)) though I would generally recommend against modifying existing variables.

Contrary to what your question title states, there’s no difference in behaviour between R scripts and the R console. This is a fundamental, universal R behaviour.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214