如何从R中上传的CSV文件替换矩阵中的值? [英] How do I replace values in a matrix from an uploaded CSV file in R?

查看:175
本文介绍了如何从R中上传的CSV文件替换矩阵中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些是我采取的步骤:

1)读取CSV文件

rawdata <- read.csv('name of my file', stringsAsFactors=FALSE)

2)通过删除基于x标准的某些记录来清理我的数据

2) Cleaned my data by removing certain records based on x-criteria

data <- rawdata[!(rawdata$YOURID==""), all()]
data <- data[(data$thiscolumn=="right"), all()]
data <- data[(data$thatcolumn=="right"), all()]

3)现在,我想用数字替换整个矩阵中的某些值(用数字值替换字符串).我尝试了以下命令,但没有任何效果(我尝试了gsubreplace):

3) Now I want to replace certain values throughout the whole matrix with a number (replace a string with a number value). I have tried the following commands and nothing works (I've tried gsub and replace):

gsub("Not the right string", 2, x, ignore.case = FALSE, perl = FALSE, fixed = FALSE, useBytes = FALSE)
data <- replace(data, data$thiscolumn == "Not the right string" , 2)
gsub("\\Not the right string", "2", data$thiscolumn, ignore.case = FALSE, perl = FALSE, fixed = FALSE, useBytes = FALSE)

我是R的新手.我通常使用C ++编写代码.我要尝试的唯一另一件事是for循环.我可能只希望R查看某些列以替换某些值,但我希望在整个矩阵中进行搜索.都可以.

I am new to R. I normally code in C++. The only other thing for me to try is a for loop. I potentially might only want R to look at certain columns for replace certain values, but I'd prefer a search through the whole matrix. Either is fine.

这些是每个R帮助的准则:

These are the guidelines per R Help:

sub(模式,替换,x,ignore.case = FALSE,perl = FALSE,固定= FALSE,useBytes = FALSE)
gsub(模式,替换,x,ignore.case = FALSE,perl = FALSE,固定= FALSE,useBytes = FALSE)

sub(pattern, replacement, x, ignore.case = FALSE, perl = FALSE, fixed = FALSE, useBytes = FALSE)
gsub(pattern, replacement, x, ignore.case = FALSE, perl = FALSE, fixed = FALSE, useBytes = FALSE)

替换(x,列表,值)
参数
x vector
列出索引向量
值替换值

replace(x, list, values)
Arguments
x vector
list an index vector
values replacement values

示例:我想用相应的数字值替换文本"Extremely Relevant 5"或任何x文本.

Example: I want to replace the text "Extremely Relevant 5" or whatever x-text, with a corresponding number value.

推荐答案

您可以使用逻辑索引替换for循环.首先,您需要确定要替换的索引,然后为这些索引分配新值.

You can substitute the for loop by using logical indexing. First you need to identify the indices of what you want to replace, then assign the new value for these indices.

这是一个小例子.假设我们有这个向量:

Here's small example. Let's say we have this vector:

x <- c(1, 2, 99, 4, 2, 99)
# x
# [1]  1  2 99  4  2 99

我们要查找所有位置均为99的地方并将其替换为0.应用x == 99时,您将得到TRUEFALSE向量.

And we want to find all places where it's 99 and replace it with 0. when you apply x == 99 you get a TRUE and FALSE vector.

x == 99
# [1] FALSE FALSE  TRUE FALSE FALSE  TRUE

您可以将此向量用作索引,以便在满足条件的情况下分配新值.

You can use this vector as an index to assign the new value where the condition is met.

x[x == 99] <- 0
# x
# [1] 1 2 0 4 2 0

类似地,您可以使用这种方法一次性将其应用于数据框或矩阵上

Similarly you can use this approach to apply it across a dataframe or a matrix in a one-shot

df <- data.frame(col1 = c(2, 99, 3), col2 = c(99, 4, 99))
# df:
#   col1 col2
# 1    2   99
# 2   99    4
# 3    3   99

df[df==99] <- 0
# df
#   col1 col2
# 1    2    0
# 2    0    4
# 3    3    0 

对于带有字符串的数据帧,由于列可能是因素,并且您要替换的值不是级别之一,因此可能会比较棘手.您可以通过将其更改为character并应用替换项来解决该问题.

For dataframe with strings, it might be trickier since the column can be factor and the value you're trying to replace is not one of the levels. You can go around that by changing it to character and apply the replacement.

> df <- data.frame(col1 = c(2, "this string", 3), col2 = c("this string", 4, "this string"))
> df
         col1        col2
1           2 this string
2 this string           4
3           3 this string
> sapply(df, class)
    col1     col2 
"factor" "factor" 

> df <- sapply(df, as.character)
> df
     col1          col2         
[1,] "2"           "this string"
[2,] "this string" "4"          
[3,] "3"           "this string"

> df[df == "this string"] <- 0
> df <- as.data.frame(df)
> df
  col1 col2
1    2    0
2    0    4
3    3    0

这篇关于如何从R中上传的CSV文件替换矩阵中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆