简体   繁体   中英

How can I randomize a CSV file in R?

I am relatively new to R, and I am trying to figure out how to take a csv that contains a list of words and randomize it. I need to create a map of objects and I want to figure out how to randomly populate the map with my list, or to simply randomize the list so I can populate the map randomly. What command can I use to do this?

Use sample() function, to shuffles rows in CSV file ( data.frame ) or shuffling a vector of objects

Input

If input CSV file has multiple rows

Test <- read.table("MapTest.csv", header = FALSE, sep = ",")
Test
#       V1    V2     V3
# 1 Heaven   Air Flower
# 2  Earth  Lily   Jazz
# 3   Hell Dance Tennis
# 4  Sweet  Sour Bitter

dim(Test)
# [1] 4 3

Shuffling rows

Running it multiple times, shuffles differently every time

  Test[sample(1:nrow(Test)),]
  #       V1    V2     V3
  # 2  Earth  Lily   Jazz
  # 4  Sweet  Sour Bitter
  # 3   Hell Dance Tennis
  # 1 Heaven   Air Flower

  Test[sample(1:nrow(Test)),]
  #       V1    V2     V3
  # 3   Hell Dance Tennis
  # 1 Heaven   Air Flower
  # 2  Earth  Lily   Jazz
  # 4  Sweet  Sour Bitter

Selecting 2 rows after shuffling

Selecting 2 rows after shuffling number of rows in the CSV file. Running it multiple times will result in different 2 rows each time.

  Test[sample(1:nrow(Test), 2),]
  #       V1   V2     V3
  # 4  Sweet Sour Bitter
  # 1 Heaven  Air Flower

  Test[sample(1:nrow(Test), 2),]
  #      V1    V2     V3
  # 4 Sweet  Sour Bitter
  # 3  Hell Dance Tennis

Shuffling objects of each row in Multiple row CSV file

  lapply(1:nrow(Test), function(x) sample(Test[x,]))
  # [[1]]
  #       V1     V3  V2
  # 1 Heaven Flower Air
  # 
  # [[2]]
  #     V3   V2    V1
  # 2 Jazz Lily Earth
  # 
  # [[3]]
  #      V2     V3   V1
  # 3 Dance Tennis Hell
  # 
  # [[4]]
  #       V3    V1   V2
  # 4 Bitter Sweet Sour


Input

If Input has 1 row: Objects comma separated

  Test <- read.table("MapTest.csv", header = FALSE, sep = ",")
  dim(Test)
  # [1]  1 12
  Test
  #       V1  V2     V3    V4   V5   V6   V7    V8     V9   V10  V11    V12
  # 1 Heaven Air Flower Earth Lily Jazz Hell Dance Tennis Sweet Sour Bitter

Shuffling objects of a CSV file with 1 row

It will results different if ran multiple times.

  sample(Test)
  #     V5    V4     V1   V6    V8     V9  V2   V10    V12  V11   V7     V3
  # 1 Lily Earth Heaven Jazz Dance Tennis Air Sweet Bitter Sour Hell Flower

  sample(Test)
  #      V4   V7     V1    V12     V3  V11    V8   V5   V10     V9   V6  V2
  # 1 Earth Hell Heaven Bitter Flower Sour Dance Lily Sweet Tennis Jazz Air


If No Input CSV File

Create a vector of objects in R

  Map_vec <- c("Hell","Heaven","Flower","Water","Air","Sky")
  Map_vec
  # [1] "Hell"   "Heaven" "Flower" "Water"  "Air"    "Sky"   

  length(Map_vec)
  # [1] 6

Shuffling Vector

  sample(Map_vec)
  # [1] "Flower" "Sky"    "Heaven" "Air"    "Hell"   "Water" 

  sample(Map_vec)
  # [1] "Water"  "Heaven" "Sky"    "Hell"   "Air"    "Flower"

Shuffling and selecting few from vector

  sample(Map_vec, 3)
  # [1] "Heaven" "Sky"    "Hell"  

  sample(Map_vec, 3)
  # [1] "Heaven" "Sky"    "Air" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM