我想更改除特定列之外的所有列的数据框中的所有值。我该怎么做?
问题1: 我的下面代码会更改数据框中找到的所有值。但是我不想在C列中更改。
df <- data.frame(list(A=c(12,14,13), B=c(16,13,18)),c=c(13,20,21))
df[df == 13]<-NA
print(df)
我也尝试过这样的事情:
df <- data.frame(list(A=c(12,14,13), B=c(16,13,18)),c=c(13,20,21))
df[df[,1:ncol(df)-1] == 13]<-NA
print(df)
但他们没有正常工作。
问题2: 另外,如果在C列中找到13,我想在所有行中将值更改为NA。 因此,数据框架将如下所示:
A . B . C
NA NA 13
14 13 20
13 18 21
答案 0 :(得分:3)
您需要定义要应用条件的data.frame的哪个子集,以及在该条件下定义该子集。
此外,$('#submit').click(function(e) {
var creds = "client_id=sensorcloud-2.2.1-SNAPSHOT&grant_type=password&client_secret=b6b4f0ec-9936-46a2-9f40-69c207e2e0f2&username=" + $('#username')[0].value +"&password=" + $('#password')[0].value;
$.ajax({
url: 'https://localhost:8445/auth/realms/sensorcloud-auth/protocol/openid-connect/token',
data: creds,
headers: {'Content-Type':'application/x-www-form-urlencoded'},
type: 'POST',
success: function(data){
localStorage.setItem('currentUser', JSON.stringify(data));
window.location.replace("https://localhost:8443/sensorcloud-2.2.1-SNAPSHOT/dashboard.html");
},
error: function() {
alert("Invalid username or password");
}
});
});
从所有序列中减去1,因此您将获得(在这种情况下)<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>SensorCloud</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<login-config>
<auth-method>KEYCLOAK</auth-method>
<realm-name>sensorcloud-auth</realm-name>
</login-config>
<context-param>
<param-name>resteasy.role.based.security</param-name>
<param-value>true</param-value>
</context-param>
<security-constraint>
<web-resource-collection>
<web-resource-name>sensorcloud-2.2.1-SNAPSHOT</web-resource-name>
<url-pattern>/rest/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>user</role-name>
<role-name>admin</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<security-constraint>
<web-resource-collection>
<web-resource-name>sensorcloud-2.2.1-SNAPSHOT</web-resource-name>
<url-pattern>/index.html</url-pattern>
<url-pattern>/help.html</url-pattern>
<url-pattern>/register.html</url-pattern>
<url-pattern>/login.html</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<security-constraint>
<web-resource-collection>
<web-resource-name>sensorcloud-2.2.1-SNAPSHOT</web-resource-name>
<url-pattern>/dashboard.html</url-pattern>
<url-pattern>/management.html</url-pattern>
<url-pattern>/password.html</url-pattern>
<url-pattern>/user.html</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>user</role-name>
<role-name>admin</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<security-constraint>
<web-resource-collection>
<web-resource-name>sensorcloud-2.2.1-SNAPSHOT</web-resource-name>
<url-pattern>/admin.html</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>admin</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<security-role>
<role-name>admin</role-name>
</security-role>
<security-role>
<role-name>user</role-name>
</security-role>
</web-app>
和1:ncol(df)-1
而不是0
和1
。
1
对于你的问题的第二部分,可以使用相同的方法;您仅在2
列上应用条件(如果需要,您可以使用df[,1:(ncol(df)-1)][df[,1:(ncol(df)-1)]==13] <-NA
# A B c
# 1 12 16 13
# 2 14 NA 20
# 3 NA 18 21
代替c
)并仅在前两列中替换值:
df[,ncol(df)]
答案 1 :(得分:3)
indices = which(df == "13", arr.ind = TRUE)
replace(df, indices[names(df)[indices[,2]]!= "c",], NA)
# A B c
#1 12 16 13
#2 14 NA 20
#3 NA 18 21
replace(df, cbind(which(df$c == "13"), (1:NCOL(df))[-which(names(df) == "c")]), NA)
# A B c
#1 NA NA 13
#2 14 13 20
#3 13 18 21
答案 2 :(得分:2)
除了C dplyr
library(dplyr)
df %>% mutate_at(vars(-one_of("c")), recode, `13`=NA_real_)
您可以使用
将C == 13的行的所有值设置为NAdf[df$c==13,setdiff(names(df), "c")]<-NA
或使用dplyr
notc <- setdiff(names(df), "c")
df[df$c==13,notc]<-df[df$c==13,notc] %>% mutate_all(function(x) NA)