是否有一个载体内访问的索引的方式 [英] Is there a way to access an index within a vector

查看:128
本文介绍了是否有一个载体内访问的索引的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个载体内访问指数(个体值)。我以为这将是类似于:

i need to access an index (individual value) within a vector. I assumed it would be similar to:

v1 <- c(a,b,c,d,e)
v1[3] = h

但是,这似乎并没有在所有的工作,任何人都知道如何做到这一点?

But that doesn't seem to work at all, anyone else know how to do this?


好吧回答大家的一些问题,这里是什么我试图实现整体大局:

Ok to answer some of your questions, here is the larger picture of what i am trying to achieve overall:

MyDataR1 <- scan("myd1.txt",what='character') 
MyDataR2 <- scan("myd2.txt",what='character') 
MyDataR3 <- scan("myd3.txt",what='character')
MyDataR4 <- scan("myd4.txt",what='character') 

AmpsR1 <- vector(mode='numeric',length=length(MyDataR1)-1)
AmpsR2 <- vector(mode='numeric',length=length(MyDataR2)-1)
AmpsR3 <- vector(mode='numeric',length=length(MyDataR3)-1)
AmpsR4 <- vector(mode='numeric',length=length(MyDataR4)-1)

AmpsR <- list(AmpsR1,AmpsR2,AmpsR3,AmpsR4)
DatesR <- list(DatesR1,DatesR2,DatesR3,DatesR4)
MyDataR <- list(MyDataR1,MyDataR2,MyDataR3,MyDataR4)

for (m in 1:length(MyDataR)){
mode(MyDataR[m])
length(MyDataR[m])
for (i in 2:length(MyDataR[m])){ 
temp <- unlist(strsplit(MyDataR[[m]][i],',')) 
DatesR[[m]][i-1] <- temp[2] 
if (length(temp) == 7) 
AmpsR[[m]][i-1] <- as.numeric(temp[6]) 
else 
AmpsR[[m]][i-1] <- NA 

end 

(我改变了我的战术在一个点用C()函数,而不是创建列表,然而这也不能工作)。
所以我想在列表中的每个向量的每个值之间迭代并设置它的价值,但是当我打印AmpsR后的循环部分,所有的值都是0.000。我知道这是读数据是有效的,当我打印MyDataR1它充满了所有相关的值。

(I changed my tactic at one point to use the "c()" function instead of creating lists, however that didn't work either). So i'm trying to iterate between each value of each vector in the list and set it's value, however when i print "AmpsR" after the for loop section, all the values are "0.000". I know the data it is reading is valid as when i print "MyDataR1" it is filled with all the relevant values.

对于这样做的更好的办法的任何意见或建议?
感谢您的输入每个人。

Any ideas or suggestions of a better way for doing this?? Thanks for your input everyone.

道歉了类似的帖子,询问另一个问题,当我说,这只是这个问题并没有直接解决我遇到的问题。无论看着从他们两人的答案,我现在已经有了更好的了解列表和向量是如何工作的,我设法解决我的问题(解决方案非常相似,Andrie与回答)。

Apologies for the similar post I added when asking another question, it was just that this question didn't directly address the issue I was having. Regardless looking at the answers from both of them I've now gained a better understanding of how lists and vectors work and I managed to solve my issue (solution very similar to what Andrie answered with).

感谢您的帮助大家。

推荐答案

这不是从你的问题你的问题是什么显而易见的,但我想这是下列之一:

It's not obvious from your question exactly what your problem is, but I'll guess it's one of the following:


  1. 您没有正确引用你的字符串。

  2. 您正在使用的变量不存在。

  3. 您实际上意味着创建一个列表,而不是串联的vector

方法1:使用引号来表示字符向量(串)

v1 <- c("a", "b", "c", "d", "e")
v1[3] <- "h"
v1
[1] "a" "b" "h" "d" "e"

选项2:您还没有定义的变量A-E和H

> a <- 1
> b <- 2
> c <- 3
> d <- 4
> e <- 5
> h <- 8
> v1 <- c(a,b,c,d,e)
> v1[3] = h
> v1
[1] 1 2 8 4 5

选项3:你的意思是创建和子集列表

在这种情况下,你应该使用列表而不是 C 。请记住,您使用的是双括号的索引列表。

In this case you should use list instead of c. And remember that you index a list using double square brackets.

a <- 1:5
b <- 6:10
c <- 11:15
d <- 16:20
e <- 21:25
h <- 26:30
v1 <- list(a,b,c,d,e)
v1[[3]] <- h

清单:

v1
[[1]]
[1] 1 2 3 4 5

[[2]]
[1]  6  7  8  9 10

[[3]]
[1] 26 27 28 29 30

[[4]]
[1] 16 17 18 19 20

[[5]]
[1] 21 22 23 24 25

该列表的要素3:

Element 3 of the list:

v1[[3]]
[1] 26 27 28 29 30

这篇关于是否有一个载体内访问的索引的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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