例如:
x1 <- data.frame("ID" = c(1:6),
"Position" = c("Left-Striker",
"Goalkeeper",
"Right-Striker",
"Striker",
"Centerback",
"Right-Midfield"))
返回:
ID Position
1 1 Left-Striker
2 2 Goalkeeper
3 3 Right-Striker
4 4 Striker
5 5 Centerback
6 6 Right-Midfield
我想将所有带有因子的单词“ Striker”重命名为因子“ Striker”。数据框如下所示:
ID Position
1 1 Striker
2 2 Goalkeeper
3 3 Striker
4 4 Striker
5 5 Centerback
6 6 Right-Midfield
答案 0 :(得分:2)
这会将变量存储为因数,并删除以R
为基础的未使用级别:
x1$Position[grepl("Striker",x1$Position)] <- "Striker"
x1$Position <- droplevels(x1$Position)
x1$Position
# [1] Striker Goalkeeper Striker Striker Centerback Right-Midfield
# Levels: Centerback Goalkeeper Right-Midfield Striker
您也可以执行以下操作以获得类似的效果(级别可能会有所不同):
levels(x1$Position)[grepl("Striker",levels(x1$Position))] <- "Striker"
x1$Position
# [1] Striker Goalkeeper Striker Striker Centerback Right-Midfield
# Levels: Centerback Goalkeeper Striker Right-Midfield
答案 1 :(得分:1)
我们可以使用gsub
删除第一个单词
x1$Position <- gsub('(^\\w+-)(Striker)','\\2',x1$Position)
()
用于对表达式进行分组
(^\\w+-)
第一组搜索一个或多个单词,后跟-
(Striker)
的第二组搜索为文字Striker
。
\\2
意味着丢弃第一组并返回第二组Striker
。
答案 2 :(得分:1)
这也可以通过forcats软件包来完成:
forcats::fct_collapse(x1$Position, "Striker" = c("Left-Striker", "Striker", "Right-Striker"))
答案 3 :(得分:1)
使用正则表达式
package com.example.android.mynewsx2;
import android.net.Uri;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
/**
* Created by Person on 11/08/2018.
*/
public class QueryUtils {
public boolean connTimeout = false;
static String createStringUrl() {
Uri.Builder builder = new Uri.Builder();
builder.scheme("http")
.encodedAuthority("content.guardianapis.com")
.appendPath("search")
.appendQueryParameter("order-by", "newest")
.appendQueryParameter("show-references", "author")
.appendQueryParameter("show-tags", "contributor")
.appendQueryParameter("page-size", "175")
.appendQueryParameter("q", "")
.appendQueryParameter("api-key", "c15d8295-7691-4172-a257-a7d065668eb4");
String url = builder.build().toString();
return url;
}
static URL createUrl() {
String stringUrl = createStringUrl();
try {
return new URL(stringUrl);
} catch (MalformedURLException e) {
Log.e("Queryutils", "Error creating URL: ", e);
return null;
}
}
private static String formatDate(String rawDate) {
String jsonDatePattern = "yyyy-MM-dd'T'HH:mm:ss'Z'";
SimpleDateFormat jsonFormatter = new SimpleDateFormat(jsonDatePattern, Locale.US);
try {
Date parsedJsonDate = jsonFormatter.parse(rawDate);
String finalDatePattern = "MMM d, yyy";
SimpleDateFormat finalDateFormatter = new SimpleDateFormat(finalDatePattern, Locale.US);
return finalDateFormatter.format(parsedJsonDate);
} catch (ParseException e) {
Log.e("QueryUtils", "Error parsing JSON date: ", e);
return "";
}
}
static String makeHttpRequest(URL url) throws IOException {
String jsonResponse = "";
if (url == null){
return jsonResponse;
}
HttpURLConnection urlConnection = null;
InputStream inputStream = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setReadTimeout(10000 /* milliseconds */);
urlConnection.setConnectTimeout(15000 /* milliseconds */);
urlConnection.connect();
if (urlConnection.getResponseCode() == 200){
inputStream = urlConnection.getInputStream();
jsonResponse = readFromStream(inputStream);
} else {
Log.e("mainActivity", "Error response code: " + urlConnection.getResponseCode());
}
} catch (IOException e) {
Log.e("Queryutils", "Error making HTTP request: ", e);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (inputStream != null) {
inputStream.close();
}
}
return jsonResponse;
}
private static String readFromStream(InputStream inputStream) throws IOException {
StringBuilder output = new StringBuilder();
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
BufferedReader reader = new BufferedReader(inputStreamReader);
String line = reader.readLine();
while (line != null) {
output.append(line);
line = reader.readLine();
}
}
return output.toString();
}
static List<News> parseJson(String response) {
ArrayList<News> listOfNews = new ArrayList<>();
try {
JSONObject jsonResponse = new JSONObject(response);
JSONObject jsonResults = jsonResponse.getJSONObject("response");
JSONArray resultsArray = jsonResults.getJSONArray("results");
for (int i = 0; i < resultsArray.length(); i++) {
JSONObject oneResult = resultsArray.getJSONObject(i);
String webTitle = oneResult.getString("webTitle");
String url = oneResult.getString("webUrl");
String date = oneResult.getString("webPublicationDate");
date = formatDate(date);
String section = oneResult.getString("sectionName");
JSONArray tagsArray = oneResult.getJSONArray("tags");
String author = "";
if (tagsArray.length() == 0) {
author = null;
} else {
for (int j = 0; j < tagsArray.length(); j++) {
JSONObject firstObject = tagsArray.getJSONObject(j);
author += firstObject.getString("webTitle") + ". ";
}
}
listOfNews.add(new News(webTitle, author, url, date, section));
}
} catch (JSONException e) {
Log.e("Queryutils", "Error parsing JSON response", e);
}
return listOfNews;
}
}
来自stringr
[]中可以使用许多预构建的类:
- [:alpha:]:字母。
library(stringr)
x1$Position <- str_replace_all(x1$Position, "[:alpha:]+-?Striker", "Striker")
表示一个或多个。 +
在您的字符串中。