how to print each value in each column just by loop - r

Below is my code:
my.dataset1<- data.frame(site=c(11,12,13,14),
season=c(21,22,23,24),
PH=c(1,2,3,4))
for i in names(my.dataset1){
for (j in nrow(my.dataset1)) {
print(my.dataset1$i[j])
}
}
What i want is that it can print the results:
11
12
13
14
21
22
23
24
1
2
3
4
what I actually get is
null
It does not work. I want to get the results just by for loop!

Loop syntax must be modified. names in the first line of loop should be also replaced with ncol(). This will work for you.
my.dataset1<- data.frame(site=c(11,12,13,14),
season=c(21,22,23,24),
PH=c(1,2,3,4))
for (i in 1:ncol(my.dataset1)){
for (j in 1:nrow(my.dataset1)) {
print(my.dataset1[j,i])
}
}

Related

How to get the last elements of the values of selected keys of a dictionary in DolphinDB?

dt=dict(STRING,ANY)
dt[`AAPL]=10 11 12
dt[`AMZN]=61 62 63
dt[`NFLX]=34 35 36 37;
For this dictionary in DolphinDB, how can I take the last element of the value for AAPL and NFLX to get the vector [12,37]?
Try this:
def f(d, symbols){
return each(x->d[x].tail(), symbols)
}
f(dt, `AAPL`NFLX);

Nested loops in R from columns of airplane routes

I am trying to make a double nested loop in R. The source is stored in routes and looks like this:
Airline AirlineID SourceAirport SourceAirportID DestinationAirport DestinationAirportID Codeshare Stops Equipment
Where every row is a flight. I am concerned with the SourceAirportID and the DestinationAirportID. My double nested loop should have the first index as the SourceAirportID to access a list of DestinationPortIDs. The nested loop needs to be of variable size because airports will not all have the same number of destinations. Here is my attempt:
graph <- list()
for (i in 1:11922) {
graph[i] <- list()
}
it <- 1
sid <- as.numeric(routes[1,4])
for (i in 1:length(routes$SourceAirportID)) {
if (sid != as.numeric((routes[i,4]))) {
sid <- as.numeric(routes[i,4])
it <- 1
}
else {
it <- it + 1
}
graph[sid][it] <- routes[sid,6]
}
Here are the first 4 rows of routes:
Airline AirlineID SourceAirport SourceAirportID DestinationAirport DestinationAirportID Codeshare Stops Equipment
17313 CG 1308 GKA 1 HGU 3 0 DH8 DHT
17314 CG 1308 GKA 1 LAE 4 0 DH8
17315 CG 1308 GKA 1 MAG 2 0 DH8
17316 CG 1308 GKA 1 POM 5 0 DH8
So I'm trying to get the list at graph[1] to contain 3 4 2 5. Instead graph[1] is null, graph[2] contains 4, graph[3] contains 2 and graph[3] contains 5. My code also throws over 50 warnings, so clearly I am doing something very wrong.
Appreciate the help!
I'm not sure if this is what you want:
#reproduciable data:
routes <- data.frame(SourceAirport=c('a','a','b','c','a','b','b','c'),
DestinationAirport=c('Q','W','D','D','Q','E','R','T'))
View(routes)
graph <- list()
o <- unique(routes$SourceAirport)
#DestinationAirports of routes that share the same SourceAirport will be saved within an element in graph
for (i in 1:length(o)) {
graph[[i]] <- routes$DestinationAirport[routes$SourceAirport==o[i]]
}

How to recode a variable into another variable

I am trying to recode the following variable:
str(dades$Edat)
num [1:30000] 24 26 34 37 57 37 29 23 28 35 ...
Into this:
agrupar.edat<-function(x){
for (i in 1:length(x)){
if (x[i]>=21 & x[i]<30) {x[i]<-'1'} else
if (x[i]>=30 & x[i]<40) {x[i]<-'2'} else
if (x[i]>=40 & x[i]<50) {x[i]<-'3'} else
if (x[i]>=50 & x[i]<60) {x[i]<-'4'} else
if (x[i]>=60 & x[i]<70) {x[i]<-'5'} else
if (x[i]>=70 & x[i]<80) {x[i]<-'6'}
}
So I can put the results here:
edx<-agrupar.edat(dades$Edat)
But something is not working and edx keeps returning me "NULL"
Problem 1.
Your function has no return argument.
As a result, it reads that way:
agrupar.edat<-function(x){
# do stuff
# good bye
}
… so logically enough, nothing (NULL) comes out of it.
Try simply adding return(1) at the end, just before the closing bracket, and magic will happen.
Note, however, that your problem does not require a function. It requires…
Problem 2.
… using cut, as #akrun's comment instructs you to do.

Error in lis[[i]] : attempt to select less than one element

This code is meant to compute the total distance of some given coordinates, but I don't know why it's not working.
The error is: Error in lis[[i]] : attempt to select less than one element.
Here is the code:
distant<-function(a,b)
{
return(sqrt((a[1]-b[1])^2+(a[2]-b[2])^2))
}
totdistance<-function(lis)
{
totdis=0
for(i in 1:length(lis)-1)
{
totdis=totdis+distant(lis[[i]],lis[[i+1]])
}
totdis=totdis+distant(lis[[1]],lis[[length(lis)]])
return(totdis)
}
liss1<-list()
liss1[[1]]<-c(12,12)
liss1[[2]]<-c(18,23)
liss1[[4]]<-c(29,25)
liss1[[5]]<-c(31,52)
liss1[[3]]<-c(24,21)
liss1[[6]]<-c(36,43)
liss1[[7]]<-c(37,14)
liss1[[8]]<-c(42,8)
liss1[[9]]<-c(51,47)
liss1[[10]]<-c(62,53)
liss1[[11]]<-c(63,19)
liss1[[12]]<-c(69,39)
liss1[[13]]<-c(81,7)
liss1[[14]]<-c(82,18)
liss1[[15]]<-c(83,40)
liss1[[16]]<-c(88,30)
Output:
> totdistance(liss1)
Error in lis[[i]] : attempt to select less than one element
> distant(liss1[[2]],liss1[[3]])
[1] 6.324555
Let me reproduce your error in a simple way
>list1 = list()
> list1[[0]]=list(a=c("a"))
>Error in list1[[0]] = list(a = c("a")) :
attempt to select less than one element
So, the next question is where are you accessing 0 index list ?
(Indexing of lists starts with 1 in R )
As Molx, indicated in previous posts : "The : operator is evaluated before the subtraction - " . This is causing 0 indexed list access.
For ex:
> 1:10-1
[1] 0 1 2 3 4 5 6 7 8 9
>1:(10-1)
[1] 1 2 3 4 5 6 7 8 9
So replace the following lines of your code
>for(i in 1:(length(lis)-1))
{
totdis=totdis+distant(lis[[i]],lis[[i+1]])
}

Read a column value from previous line and next line but insert them as additional fields in the current line using awk

I hope you can help me out with my problem.
I have an input file with 3 columns of data which looks like this:
Apl_No Act_No Sfx_No
100 10 0
100 11 1
100 12 2
100 13 3
101 20 0
101 21 1
I need to create an output file which contains the data as in the input and 3 additional fileds in its output. It should look like this:
Apl_No Act_No Sfx_No Crt_Act_No Prs_Act_No Cd_Act_No
100 10 0 - - -
100 11 1 10 11 12
100 12 2 11 12 13
100 13 3 12 13 10
101 20 0 - - -
101 21 1 20 21 20
Every Apl_No has a set of Act_No that are mapped to it. 3 new fields need to be created: Crt_Act_No Prs_Act_No Cd_Act_No. When the first unique Apl_No is encountered the column values 4, 5 and 6 (Crt_Act_No Prs_Act_No Cd_Act_No) need to be dashed out. For every following occurrence of the same Apl_No the Crt_Act_No is the same as the Act_No on the previous line, the Prs_Act_No is same as the Act_No on the current line and the Cd_Act_No is same as the Act_No on the next line. This continues for all the following rows bearing the same Apl_No except for the last row. In the last row the Crt_Act_No and Prs_Act_No is filled in the same way as the above rows but the Cd_Act_No needs to be pulled from the Act_No from the first row when the first unique Apl_No is encountered.
I wish to achieve this using awk. Can anyone please help me out how to go about this.
One solution:
awk '
## Print header in first line.
FNR == 1 {
printf "%s %s %s %s\n", $0, "Crt_Act_No", "Prs_Act_No", "Cd_Act_No";
next;
}
## If first field not found in the hash means that it is first unique "Apl_No", so
## print line with dashes and save some data for use it later.
## "line" variable has the content of the previous iteration. Print it if it is set.
! apl[ $1 ] {
if ( line ) {
sub( /-/, orig_act, line );
print line;
line = "";
}
printf "%s %s %s %s\n", $0, "-", "-", "-";
orig_act = prev_act = $2;
apl[ $1 ] = 1;
next;
}
## For all non-unique "Apl_No"...
{
## If it is the first one after the line with
## dashes (line not set) save it is content in "line" and the variable
## that I will have to check later ("Act_No"). Note that I leave a dash in last
## field to substitute in the following iteration.
if ( ! line ) {
line = sprintf( "%s %s %s %s", $0, prev_act, $2, "-" );
prev_act = $2;
next;
}
## Now I know the field, so substitute the dash with it, print and repeat
## the process with current line.
sub( /-/, $2, line );
print line;
line = sprintf( "%s %s %s %s", $0, prev_act, $2, "-" );
prev_act = $2;
}
END {
if ( line ) {
sub( /-/, orig_act, line );
print line;
}
}
' infile | column -t
That yields:
Apl_No Act_No Sfx_No Crt_Act_No Prs_Act_No Cd_Act_No
100 10 0 - - -
100 11 1 10 11 12
100 12 2 11 12 13
100 13 3 12 13 10
101 20 0 - - -
101 21 1 20 21 20

Resources