【问题标题】:How can I remove a specific symbol from for an entire column如何从整个列中删除特定符号
【发布时间】:2022-11-30 18:12:05
【问题描述】:

我想知道如何删除整个列的特定符号。这是原始数据的样子:original data

我想要得到的唯一元素是第一个词。

这是我的完整数据集的样子:

以下是数据背景信息

library("dplyr")
library("stringr")
library("tidyverse")
library("ggplot2")

# load the .csv into R studio, you can do this 1 of 2 ways
#read.csv("the name of the .csv you downloaded from kaggle")
spotiify_origional <- read.csv("charts.csv")
spotiify_origional <- read.csv("https://raw.githubusercontent.com/info201a-au2022/project-group-1-section-aa/main/data/charts.csv")
View(spotiify_origional)
# filters down the data
# removes the track id, explicit, and duration columns
spotify_modify <- spotiify_origional %>% 
  select(name, country, date, position, streams, artists, genres = artist_genres)

#returns all the data just from 2022
#this is the data set you should you on the project
spotify_2022 <- spotify_modify %>% 
  filter(date >= "2022-01-01") %>% 
  arrange(date) %>% 
  group_by(date)

spotify_2022_global <- spotify_modify %>% 
  filter(date >= "2022-01-01") %>% 
  filter(country == "global") %>% 
  arrange(date) %>% 
  group_by(streams)
View(spotify_2022_global) 

这就是我所做的,

top_15 <- spotify_2022_global[order(spotify_2022_global$streams, decreasing = TRUE), ]
top_15 <- top_15[1:15,]
top_15$streams <- as.numeric(top_15$streams)
View(top_15)  

top_15 <- top_15 %>% 
  separate(genres, c("genres"), sep = ',')
top_15$genres<-gsub("]","",as.character(top_15$genres))
View(top_15)

现在这个名字看起来像这样:

name now look like this

我尝试使用相同的 gsub 函数删除其余的括号和引号,但没有用。

我想知道此时我该怎么办?任何建议都会有很大帮助!谢谢!

【问题讨论】:

    标签: r dataframe ggplot2


    【解决方案1】:
    top_15$genres <- gsub("]|\[|[']|,","",as.character(top_15$genres))
    

    其中正则表达式 "]|\[|[']|," 使用 | 字符 OR 来匹配多个事物,即:

    • ]右方括号
    • \[左方括号
    • [']单引号
    • ,逗号(如果需要?)

    如果运行:

    top_15 <- spotify_2022_global[order(spotify_2022_global$streams, decreasing = TRUE), ]
    top_15 <- top_15[1:15,]
    top_15$streams <- as.numeric(top_15$streams)
    top_15$genres <- gsub("]|\[|[']|,","",as.character(top_15$genres))
    view(top_15)
    

    给出:

    【讨论】:

      猜你喜欢
      • 2022-11-28
      • 1970-01-01
      • 2019-09-08
      • 2021-04-15
      • 2012-01-15
      • 2018-05-29
      • 1970-01-01
      • 2020-02-12
      • 2015-12-24
      相关资源
      最近更新 更多