使用R在列之间交换值

时间:2018-09-25 01:50:54

标签: r normalization

我有一个名为cloud的数据集,如下所示:

"Rainfall, Treatment
274.7, Seeded
274.7, Seeded
Seeded, 255
242.5, Seeded
200.7, Seeded
198.6, Seeded
129.6, Seeded
119, Seeded
118.3, Seeded
115.3, Seeded
92.4, Seeded
40.6, Seeded
32.7, Seeded
31.4, Seded
17.5, Seeded"

有人可以帮我吗?

  1. 将数据交换到错误放置值的位置(即应交换Rainfall == "Seeded"Treatment == 255);和

  2. Treatment == "Seded"中的值的拼写正确为"Seeded"

3 个答案:

答案 0 :(得分:1)

概述

我将放错位置的值存储在两个单独的向量中。然后在private static final BasicCookieStore cookieStore = new BasicCookieStore(); private static final HttpClient client = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build(); public static void main(String[] args) { String baseUrl = "https://powerschoolinstallurl/"; String username = "username"; String password = "password"; try { // get hidden data fields, calc hmac data HashMap<String, String> result = getAuthCodes(baseUrl); String dbpwField = getDBPWField(result.get("contextData"), password); String pwField = getPWField(result.get("contextData"), password); List<NameValuePair> form = new ArrayList<>(); form.add(new BasicNameValuePair("pstoken", result.get("pstoken"))); form.add(new BasicNameValuePair("contextData", result.get("pstoken"))); form.add(new BasicNameValuePair("dbpw", dbpwField)); form.add(new BasicNameValuePair("serviceName", "PS Parent Portal")); form.add(new BasicNameValuePair("pcasServerURL", "/")); form.add(new BasicNameValuePair("credentialType", "User Id and Password Credential")); form.add(new BasicNameValuePair("account", username)); form.add(new BasicNameValuePair("pw", pwField)); form.add(new BasicNameValuePair("ldappassword", password)); UrlEncodedFormEntity requestEntity = new UrlEncodedFormEntity(form); HttpPost postMethod = new HttpPost(baseUrl + "guardian/home.html"); postMethod.setEntity(requestEntity); HttpResponse rawResponse = client.execute(postMethod); System.out.println(rawResponse.getStatusLine().getStatusCode()); try { String responseString = new BasicResponseHandler().handleResponse(rawResponse); System.out.println(responseString); } catch (HttpResponseException ignore) {} System.out.println(cookieStore.getCookies().toString()); HttpGet getMethod = new HttpGet(baseUrl + "guardian/home.html"); // replicating headers, result is the same nontheless getMethod.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); getMethod.setHeader("Accept-Encoding", "gzip, deflate, br"); getMethod.setHeader("Accept-Language", "en-US,en;q=0.5"); getMethod.setHeader("Referer", "https://powerschoolinstallurl/public/home.html"); getMethod.setHeader("DNT", "1"); getMethod.setHeader("Connection", "keep-alive"); getMethod.setHeader("Cache-Control", "no-cache"); getMethod.setHeader("Host", "ps.install.domain"); getMethod.setHeader("Upgrade-Insecure-Requests", "1"); getMethod.setHeader("Pragma", "no-cache"); HttpResponse resp2 = client.execute(getMethod); String responseString2 = new BasicResponseHandler().handleResponse(resp2); System.out.println(responseString2); } catch (IOException e) { e.printStackTrace(); } } 内部使用了三个dplyr::if_else()调用,以根据需要清除变量。

dplyr::mutate()

答案 1 :(得分:0)

您需要一个临时人物来交换

temp                <- cloud$Treatment[38]
cloud$Treatment[38] <- cloud$Rainfall[38]
cloud$Rainfall[38]  <- temp
temp                <- NULL

您还可以使用此方法更改拼写:

cloud$Treatment[49] <- "Seeded"

答案 2 :(得分:0)

使用一个较小的示例

df <- data.frame(Rainfall=c('Seeded', '31.4'),
                 Treatment=c('255', 'Seded'),
                 stringsAsFactors = F)
df

  Rainfall Treatment
1   Seeded       255
2     31.4     Seded

可能的解决方案:

# Swap values from col/col2 on row 1 (changing col order)
df[1, c(1,2)] <- df[1, c(2,1)]
# Rename Treatment value on row 2
df[2, c("Treatment")] <- 'Seeded'

df 

  Rainfall Treatment
1      255    Seeded
2     31.4    Seeded